我想知道为什么如果没有将代码标记为1
,即使结果不是null
,应用程序也会继续运行并且永不停止,并且标记为2
的代码不会出现1
。打印预期结果。
但是,如果代码标记为null
,则应用程序在结果不是2
时结束,并打印标记为private Object response=null;
public Object getResponse() {
return response;
}
@Override
public void onComplete(Object response) {
this.response=response;
}
@Override
public void onError(Exception exception) {
exception.printStackTrace();
}
的代码。
AsyncMethodCallback
public static void main(String[] args) {
try {
TNonblockingServerSocket serverSocket=new TNonblockingServerSocket(10005);
Hello.Processor<Hello.Iface> processor=new Hello.Processor<>(new HelloServiceImpl());
TNonblockingServer.Args serverArgs=new TNonblockingServer.Args(serverSocket);
TProtocolFactory factory=new TBinaryProtocol.Factory();
serverArgs.transportFactory(new TFramedTransport.Factory());
serverArgs.processor(processor);
serverArgs.protocolFactory(factory);
TServer server=new TNonblockingServer(serverArgs);
System.out.println("server start....");
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
}
异步服务器
public static void main(String[] args) {
try {
TAsyncClientManager clientManager=new TAsyncClientManager();
TNonblockingSocket socket=new TNonblockingSocket("localhost",10005);
TProtocolFactory protocolFactory=new TBinaryProtocol.Factory();
Hello.AsyncClient asyncClient=new Hello.AsyncClient(protocolFactory,clientManager,socket);
System.out.println("Client calls....");
MethodCallback<Integer> methodCallback=new MethodCallback<>();
asyncClient.helloInt(14,methodCallback);
Object result=methodCallback.getResponse();
while (result==null){
result=methodCallback.getResponse();
// System.out.println("result is "+result); //1
}
System.out.println(result); //2
} catch (IOException | TException e) {
e.printStackTrace();
}
}
异步客户端
7a4730f8f948471dbc77f6f71a250f87