我想在我的主线程中定义我的runnable
,然后将它作为参数传递给另一个线程,如下所示 -
final Runnable runnable = new Runnable() {
@Override
public void run() {
Log.i("Running runnable","");
}
};
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
text.setText(msg.obj.toString());
}
};
ThreadTest handlerThreadTest = new ThreadTest(runnable, handler);
handlerThreadTest.start();
ThreadTest
类的定义如下 -
public class ThreadTest extends Thread {
private Handler handler;
private Runnable runnable;
public ThreadTest(Runnable runnable, Handler handler) {
this.handler = handler;
this.runnable = runnable;
}
@Override
public void run() {
super.run();
runnable.run();
Message msg = handler.obtainMessage();
msg.obj = "YUSSSSSS!";
handler.sendMessage(msg);
} }
我能够将“YUSSSSSS”消息发送给我的处理程序,但runnable.run()
似乎没有工作,因为我在logcat中看不到任何内容。
我在哪里弄错了?
答案 0 :(得分:1)
Logcat倾向于删除空消息,在日志中添加一些字符,您可能会在“运行runnable”标签下看到一些内容
Log.i("RunnableImpl", "Running runnable");