gpt4 book ai didi

java - 在自定义线程类中执行可运行参数

转载 作者:行者123 更新时间:2023-12-02 12:12:45 24 4
gpt4 key购买 nike

我想在主线程中定义我的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 中看不到任何内容。

我哪里弄错了?

最佳答案

Logcat 倾向于删除空消息,在日志中添加一些字符,您可能会在“Running runnable”标签下看到一些内容

Log.i("RunnableImpl", "Running runnable");

关于java - 在自定义线程类中执行可运行参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46412068/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com