一个方法中含有一个新的线程的执行顺序,留下以后万一忘了可以来看看

来源:互联网 发布:sql constraint 编辑:程序博客网 时间:2024/04/26 08:19

在一个点击事件中拥有一个thread线程1,在运行方法里面的操作的时候线程1将会最后运行,也就是说在运行完成点击事件里面的所有操作之后再去运行那个线程1。

如果线1程里面又套了一个新的线程2,同样的,会执行完线程1再去执行线程2。
下面方法执行后提醒的顺序为:1–5–6–2–4–3

bt.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View arg0) {                Log.v("提醒——————————————————————", "1");                Thread thread = new Thread(){                    @Override                    public void run() {                        Log.v("提醒——————————————————————", "2");                        Thread t = new Thread(){                            public void run() {                                Log.v("提醒——————————————————————", "3");                            };                        };                        t.start();                        Log.v("提醒——————————————————————", "4");                    }                };                thread.start();                Log.v("提醒——————————————————————", "5");                jx();            }        });    }    public void jx(){        Log.v("提醒——————————————————————", "6");    }

在调用jx方法的时候,如果需要传值,但是值是在线程里面获取的则调用应该放在 提醒4的位置,,如果和上面代码一样调用则会发生空指针异常。如下:

Thread thread = new Thread(){                    @Override                    public void run() {                        try {                            String URL = "http://www.kuaidi100.com/query?type=ems&postid="+et.getText().toString();                            Log.v("URL______________", URL);                            HttpClient client = new DefaultHttpClient();                            HttpPost hp = new HttpPost(URL);                            HttpResponse hr = client.execute(hp);                            HttpEntity he = hr.getEntity();                            out = EntityUtils.toString(he);                        } catch (Exception e) {                            e.printStackTrace();                        }                    }                };                thread.start();                jxjson(out);            }

这样在jxjson(out);位置会发生空指针异常。原因很简单,现在out是空的,由于thread线程会最后运行,所以在jxjson(out);调用的时候实际上还没有运行thread。所以out是空的。

正确调用如下:

Thread thread = new Thread(){                    @Override                    public void run() {                        try {                            String URL = "http://www.kuaidi100.com/query?type=ems&postid="+et.getText().toString();                            Log.v("URL______________", URL);                            HttpClient client = new DefaultHttpClient();                            HttpPost hp = new HttpPost(URL);                            HttpResponse hr = client.execute(hp);                            HttpEntity he = hr.getEntity();                            out = EntityUtils.toString(he);                            jxjson(out);                        } catch (Exception e) {                            e.printStackTrace();                        }                    }                };                thread.start();
0 0
原创粉丝点击