Java NIO.2 CompletetionHandler 未到达
Java NIO.2 CompletetionHandler not reaching
Java NIO.2 Gurus 谁能解释为什么当我注释掉 Thread.sleep(...) 行时这段代码不起作用。
告诉 JVM 处理异步操作直到程序完成的优雅方法是什么?
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
public class Main {
public static void main(String[] args) {
try {
AsynchronousSocketChannel channel = AsynchronousSocketChannel.open();
channel.connect(new InetSocketAddress("www.yahoo.com", 80),
null, new CompletionHandler<Void, Object>() {
@Override
public void completed(Void result, Object attachment) {
System.out.println("Connected");
ByteBuffer buffer = ByteBuffer.wrap("GET http://www.yahoo.com HTTP/1.1\r\n\r\n".getBytes());
channel.write(buffer, null, new CompletionHandler<Integer, Object>() {
@Override
public void completed(Integer result, Object attachment) {
System.out.println("Write completed: " + result.toString());
ByteBuffer buffer = ByteBuffer.allocate(23);
channel.read(buffer, null, new CompletionHandler<Integer, Object>() {
@Override
public void completed(Integer result, Object attachment) {
System.out.println("Read completed: " + result.toString());
System.out.println(new String(buffer.array()));
}
@Override
public void failed(Throwable exc, Object attachment) {
exc.printStackTrace();
}
});
}
@Override
public void failed(Throwable exc, Object attachment) {
exc.printStackTrace();
}
});
}
@Override
public void failed(Throwable exc, Object attachment) {
exc.printStackTrace();
}
});
Thread.sleep(10000);
channel.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
提前致谢。
为什么删除 Thread.sleep(...) 后它不起作用:
方法connect(SocketAddress remote, A attachment, CompletionHandler<Void,? super A> handler)
创建了一个新线程,他在其中连接并调用处理程序。
因此,您将在套接字连接或所有数据传输之前立即关闭套接字。
如何避免
你必须想办法等待其他线程。
例如 java.util.concurrent.CompletableFuture<V>
:
try {
CompletableFuture<Boolean> future = new CompletableFuture<Boolean>();
AsynchronousSocketChannel channel = AsynchronousSocketChannel.open();
channel.connect(new InetSocketAddress("www.yahoo.com", 80),
null, new CompletionHandler<Void, Object>() {
@Override
public void completed(Void result, Object attachment) {
System.out.println("Connected");
ByteBuffer buffer = ByteBuffer.wrap("GET http://www.yahoo.com HTTP/1.1\r\n\r\n".getBytes());
channel.write(buffer, null, new CompletionHandler<Integer, Object>() {
@Override
public void completed(Integer result, Object attachment) {
System.out.println("Write completed: " + result.toString());
ByteBuffer buffer = ByteBuffer.allocate(23);
channel.read(buffer, null, new CompletionHandler<Integer, Object>() {
@Override
public void completed(Integer result, Object attachment) {
System.out.println("Read completed: " + result.toString());
System.out.println(new String(buffer.array()));
//
future.complete(true);
}
@Override
public void failed(Throwable exc, Object attachment) {
exc.printStackTrace();
//
future.complete(false);
}
});
}
@Override
public void failed(Throwable exc, Object attachment) {
exc.printStackTrace();
//
future.complete(false);
}
});
}
@Override
public void failed(Throwable exc, Object attachment) {
exc.printStackTrace();
//
future.complete(false);
}
});
// Wait until the other Threads are finished
System.out.println("Successs: "+future.get(10, TimeUnit.SECONDS));
channel.close();
} catch (Exception ex) {
ex.printStackTrace();
}
或者在你的情况下你可以简单地处理 AsynchronousSocketChannel
这种方式(没有处理程序):
try {
AsynchronousSocketChannel channel = AsynchronousSocketChannel.open();
Future<Void> futureConnect = channel.connect(new InetSocketAddress("www.yahoo.com", 80));
// wait until connected
futureConnect.get();
System.out.println("Connected");
ByteBuffer buffer = ByteBuffer.wrap("GET http://www.yahoo.com HTTP/1.1\r\n\r\n".getBytes());
Future<Integer> futureWrite = channel.write(buffer);
// wait until all data is written
Integer resultWrite = futureWrite.get();
System.out.println("Write completed: " + resultWrite.toString());
ByteBuffer bufferRead = ByteBuffer.allocate(23);
Future<Integer> futureRead = channel.read(bufferRead);
// wait ...
Integer resultRead = futureRead.get();
System.out.println("Read completed: " + resultRead.toString());
System.out.println(new String(bufferRead.array()));
// finnished now the channel can be closed
channel.close();
} catch (Exception e) {
e.printStackTrace();
}
Java NIO.2 Gurus 谁能解释为什么当我注释掉 Thread.sleep(...) 行时这段代码不起作用。
告诉 JVM 处理异步操作直到程序完成的优雅方法是什么?
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
public class Main {
public static void main(String[] args) {
try {
AsynchronousSocketChannel channel = AsynchronousSocketChannel.open();
channel.connect(new InetSocketAddress("www.yahoo.com", 80),
null, new CompletionHandler<Void, Object>() {
@Override
public void completed(Void result, Object attachment) {
System.out.println("Connected");
ByteBuffer buffer = ByteBuffer.wrap("GET http://www.yahoo.com HTTP/1.1\r\n\r\n".getBytes());
channel.write(buffer, null, new CompletionHandler<Integer, Object>() {
@Override
public void completed(Integer result, Object attachment) {
System.out.println("Write completed: " + result.toString());
ByteBuffer buffer = ByteBuffer.allocate(23);
channel.read(buffer, null, new CompletionHandler<Integer, Object>() {
@Override
public void completed(Integer result, Object attachment) {
System.out.println("Read completed: " + result.toString());
System.out.println(new String(buffer.array()));
}
@Override
public void failed(Throwable exc, Object attachment) {
exc.printStackTrace();
}
});
}
@Override
public void failed(Throwable exc, Object attachment) {
exc.printStackTrace();
}
});
}
@Override
public void failed(Throwable exc, Object attachment) {
exc.printStackTrace();
}
});
Thread.sleep(10000);
channel.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
提前致谢。
为什么删除 Thread.sleep(...) 后它不起作用:
方法connect(SocketAddress remote, A attachment, CompletionHandler<Void,? super A> handler)
创建了一个新线程,他在其中连接并调用处理程序。
因此,您将在套接字连接或所有数据传输之前立即关闭套接字。
如何避免
你必须想办法等待其他线程。
例如 java.util.concurrent.CompletableFuture<V>
:
try {
CompletableFuture<Boolean> future = new CompletableFuture<Boolean>();
AsynchronousSocketChannel channel = AsynchronousSocketChannel.open();
channel.connect(new InetSocketAddress("www.yahoo.com", 80),
null, new CompletionHandler<Void, Object>() {
@Override
public void completed(Void result, Object attachment) {
System.out.println("Connected");
ByteBuffer buffer = ByteBuffer.wrap("GET http://www.yahoo.com HTTP/1.1\r\n\r\n".getBytes());
channel.write(buffer, null, new CompletionHandler<Integer, Object>() {
@Override
public void completed(Integer result, Object attachment) {
System.out.println("Write completed: " + result.toString());
ByteBuffer buffer = ByteBuffer.allocate(23);
channel.read(buffer, null, new CompletionHandler<Integer, Object>() {
@Override
public void completed(Integer result, Object attachment) {
System.out.println("Read completed: " + result.toString());
System.out.println(new String(buffer.array()));
//
future.complete(true);
}
@Override
public void failed(Throwable exc, Object attachment) {
exc.printStackTrace();
//
future.complete(false);
}
});
}
@Override
public void failed(Throwable exc, Object attachment) {
exc.printStackTrace();
//
future.complete(false);
}
});
}
@Override
public void failed(Throwable exc, Object attachment) {
exc.printStackTrace();
//
future.complete(false);
}
});
// Wait until the other Threads are finished
System.out.println("Successs: "+future.get(10, TimeUnit.SECONDS));
channel.close();
} catch (Exception ex) {
ex.printStackTrace();
}
或者在你的情况下你可以简单地处理 AsynchronousSocketChannel
这种方式(没有处理程序):
try {
AsynchronousSocketChannel channel = AsynchronousSocketChannel.open();
Future<Void> futureConnect = channel.connect(new InetSocketAddress("www.yahoo.com", 80));
// wait until connected
futureConnect.get();
System.out.println("Connected");
ByteBuffer buffer = ByteBuffer.wrap("GET http://www.yahoo.com HTTP/1.1\r\n\r\n".getBytes());
Future<Integer> futureWrite = channel.write(buffer);
// wait until all data is written
Integer resultWrite = futureWrite.get();
System.out.println("Write completed: " + resultWrite.toString());
ByteBuffer bufferRead = ByteBuffer.allocate(23);
Future<Integer> futureRead = channel.read(bufferRead);
// wait ...
Integer resultRead = futureRead.get();
System.out.println("Read completed: " + resultRead.toString());
System.out.println(new String(bufferRead.array()));
// finnished now the channel can be closed
channel.close();
} catch (Exception e) {
e.printStackTrace();
}