2021-01-25 12:24:52 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2021 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
2021-05-20 18:57:31 +02:00
|
|
|
package io.netty.buffer.api.tests.examples;
|
2021-01-25 12:24:52 +01:00
|
|
|
|
2021-02-12 18:22:07 +01:00
|
|
|
import io.netty.buffer.api.Buffer;
|
2021-05-20 18:57:31 +02:00
|
|
|
import io.netty.buffer.api.BufferAllocator;
|
2021-01-25 12:24:52 +01:00
|
|
|
import io.netty.buffer.api.Send;
|
|
|
|
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Future;
|
|
|
|
|
|
|
|
import static java.util.concurrent.Executors.newFixedThreadPool;
|
|
|
|
import static java.util.concurrent.Executors.newSingleThreadExecutor;
|
|
|
|
|
|
|
|
public class SendExample {
|
|
|
|
|
|
|
|
static final class Ex1 {
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
ExecutorService executor =
|
|
|
|
newSingleThreadExecutor();
|
2021-09-01 11:16:31 +02:00
|
|
|
BufferAllocator allocator = BufferAllocator.onHeapUnpooled();
|
2021-01-25 12:24:52 +01:00
|
|
|
|
|
|
|
var future = beginTask(executor, allocator);
|
|
|
|
future.get();
|
|
|
|
|
|
|
|
allocator.close();
|
|
|
|
executor.shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Future<?> beginTask(
|
2021-02-12 18:22:07 +01:00
|
|
|
ExecutorService executor, BufferAllocator allocator) {
|
|
|
|
try (Buffer buf = allocator.allocate(32)) {
|
2021-01-25 12:24:52 +01:00
|
|
|
// !!! pit-fall: buffer life-time ends before task completes
|
|
|
|
return executor.submit(new Task(buf));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class Task implements Runnable {
|
2021-02-12 18:22:07 +01:00
|
|
|
private final Buffer buf;
|
2021-01-25 12:24:52 +01:00
|
|
|
|
2021-02-12 18:22:07 +01:00
|
|
|
Task(Buffer buf) {
|
2021-01-25 12:24:52 +01:00
|
|
|
this.buf = buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
// !!! danger: access out-side owning thread.
|
|
|
|
while (buf.writableBytes() > 0) {
|
|
|
|
buf.writeByte((byte) 42);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static final class Ex2 {
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
ExecutorService executor = newSingleThreadExecutor();
|
2021-09-01 11:16:31 +02:00
|
|
|
BufferAllocator allocator = BufferAllocator.onHeapUnpooled();
|
2021-01-25 12:24:52 +01:00
|
|
|
|
|
|
|
var future = beginTask(executor, allocator);
|
|
|
|
future.get();
|
|
|
|
|
|
|
|
allocator.close();
|
|
|
|
executor.shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Future<?> beginTask(
|
2021-02-12 18:22:07 +01:00
|
|
|
ExecutorService executor, BufferAllocator allocator) {
|
|
|
|
try (Buffer buf = allocator.allocate(32)) {
|
2021-01-25 12:24:52 +01:00
|
|
|
return executor.submit(new Task(buf.send()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class Task implements Runnable {
|
2021-02-12 18:22:07 +01:00
|
|
|
private final Send<Buffer> send;
|
2021-01-25 12:24:52 +01:00
|
|
|
|
2021-02-12 18:22:07 +01:00
|
|
|
Task(Send<Buffer> send) {
|
2021-01-25 12:24:52 +01:00
|
|
|
this.send = send;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2021-02-12 18:22:07 +01:00
|
|
|
try (Buffer buf = send.receive()) {
|
2021-01-25 12:24:52 +01:00
|
|
|
while (buf.writableBytes() > 0) {
|
|
|
|
buf.writeByte((byte) 42);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-26 17:13:29 +02:00
|
|
|
static final class Ex3 {
|
2021-01-25 12:24:52 +01:00
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
ExecutorService executor = newFixedThreadPool(4);
|
2021-09-01 11:16:31 +02:00
|
|
|
BufferAllocator allocator = BufferAllocator.onHeapUnpooled();
|
2021-01-25 12:24:52 +01:00
|
|
|
|
2021-02-12 18:22:07 +01:00
|
|
|
try (Buffer buf = allocator.allocate(4096)) {
|
2021-04-29 15:34:04 +02:00
|
|
|
var futA = executor.submit(new Task(buf.writerOffset(1024).split().send()));
|
|
|
|
var futB = executor.submit(new Task(buf.writerOffset(1024).split().send()));
|
|
|
|
var futC = executor.submit(new Task(buf.writerOffset(1024).split().send()));
|
2021-01-25 12:24:52 +01:00
|
|
|
var futD = executor.submit(new Task(buf.send()));
|
|
|
|
futA.get();
|
|
|
|
futB.get();
|
|
|
|
futC.get();
|
|
|
|
futD.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
allocator.close();
|
|
|
|
executor.shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class Task implements Runnable {
|
2021-02-12 18:22:07 +01:00
|
|
|
private final Send<Buffer> send;
|
2021-01-25 12:24:52 +01:00
|
|
|
|
2021-02-12 18:22:07 +01:00
|
|
|
Task(Send<Buffer> send) {
|
2021-01-25 12:24:52 +01:00
|
|
|
this.send = send;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2021-02-12 18:22:07 +01:00
|
|
|
try (Buffer buf = send.receive().writerOffset(0)) {
|
2021-01-25 12:24:52 +01:00
|
|
|
while (buf.writableBytes() > 0) {
|
|
|
|
buf.writeByte((byte) 42);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|