我正试图在Android中获得多线程。我的目标是将数据从主线程发送到工作线程。
我有一个主要活动,其onCreate
-中包含以下代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ThreadOne threadOne = new ThreadOne();
threadOne.start();
threadOneLooper = threadOne.getLooper();
Message msg = new Message();
msg.obj = "message.";
Log.i("id--(threadOneLooper)", String.valueOf(threadOneLooper.getThread().getId()));
Log.i("id--(Main)", String.valueOf(getMainLooper().getThread().getId()));
TestHandler mHandler = new TestHandler(threadOneLooper);
mHandler.handleMessage(msg);
}
正如您所看到的,我在threadOneLooper
中存储了工作线程的looper的引用,然后记录其关联线程的id。同时我也打印主线程的id。
以下是我的帖子的代码 -
public class ThreadOne extends Thread{
@Override
public void run() {
if (Looper.myLooper() == null)
Looper.prepare();
Log.i("ThreadOne", "run()");
// Just making sure all approaches give the same result
Log.i("Id--Thread id 1", String.valueOf(getLooper().getThread().getId()));
Log.i("Id--Thread id 2", String.valueOf(getId()));
Looper.loop();
}
Looper getLooper() {
if (Looper.myLooper() != null)
return Looper.myLooper();
else
return null;
}
以下是我的处理程序的代码 -
public class TestHandler extends Handler {
TestHandler(Looper myLooper) {
super(myLooper);
}
public void handleMessage(Message msg) {
String txt = (String) msg.obj;
Log.i("Handler", txt);
Log.i("Id--(Handler)", String.valueOf(getLooper().getThread().getId()));
}
}
现在,问题是我假设Log.i("Id--(Handler)", String.valueOf(getLooper().getThread().getId()));
语句将记录ThreadOne
的线程id,因为我们将线程的looper传递给处理程序。但是,记录的id是主线程。这是为什么?假设handleMessage()
正在主线程上执行是否正确?
问题在于getLooper()
的ThreadOne
方法。该方法返回与调用该方法的线程关联的myLooper()。
为了返回与ThreadOne
相关的Looper,我建议以下类的实现:
public class ThreadOne extends Thread {
private Looper mLooper = null;
@Override
public void run() {
Looper.prepare();
mLooper = Looper.myLooper();
Looper.loop();
}
Looper getLooper() {
return mLooper;
}
}
注意! ThreadOne
的Looper是null
直到它运行。你不能早点获得非null
的参考。你必须在调用check if the thread is running方法之前使用getLooper()
。
附:你可能想重新考虑获得ThreadOne
的Looper的方法。拥有与ThreadOne
相关联的处理程序可能就足够了(see the example of thread)。