gpt4 book ai didi

Java NIO.2 CompletetionHandler 未到达

转载 作者:行者123 更新时间:2023-12-02 13:00:18 26 4
gpt4 key购买 nike

Java NIO.2 大师可以解释为什么当我注释掉 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 CompletetionHandler 未到达,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44325231/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com