查看 Java 库AsyncHttpClient中的代码块,客户端启动一个新线程 (a Future
) 来发出请求。回调会发生在同一个线程上,还是会在“主”线程上运行(在这种情况下,new AsyncHttpClient()
是被调用的线程?
import com.ning.http.client.*;
import java.util.concurrent.Future;
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
asyncHttpClient.prepareGet("http://www.ning.com/ ").execute(new AsyncCompletionHandler<Response>(){
@Override
public Response onCompleted(Response response) throws Exception{
// Do something with the Response
// ...
return response;
}
@Override
public void onThrowable(Throwable t){
// Something wrong happened.
}
});