为了账号安全,请及时绑定邮箱和手机立即绑定

调用并将参数传递给另一个线程中的方法

调用并将参数传递给另一个线程中的方法

慕少森 2021-04-06 17:14:15
我已经创建了这个小项目来展示我想做的事情,但实际上,它将被用在使用大约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这可能吗?如何做呢?
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 182 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信