我已经创建了这个小项目来展示我想做的事情,但实际上,它将被用在使用大约60个不同线程的大型应用程序中。
我有两节课
public class Main {
public static void main(String[] args) {
Http http = new Http();
Thread threadHttp = new Thread(http, "httpThread1");
threadHttp.start();
http.getPage("http://google.com"); // <-- This gets called on
// the main thread,
//I want it to get called from the
// "httpThread1" thread
}
}
和
public class Http implements Runnable {
volatile OkHttpClient client;
@Override
public void run() {
client = new OkHttpClient.Builder().readTimeout(10, TimeUnit.SECONDS).retryOnConnectionFailure(true).build();
}
public void getPage(String url) {
Request request = new Request.Builder().url(url).build();
try {
Response response = client.newCall(request).execute();
System.out.println(response.body().string());
} catch (Exception e) {
e.printStackTrace();
}
}
}
从主线程开始,我希望能够调用该getPage方法,但要在httpThread1我们启动和初始化的上执行该方法OkHttpClient client
这可能吗?如何做呢?
相关分类