可能重复:need-help-returning-object-in-thread-run-method
你好。我有一个实现可运行的类,我有一个列表,存储用该类的不同对象实例化的线程。给定运行它们的线程对象,我如何访问底层对象的属性?这是一个例子:
public class SO {
public static class TestRunnable implements Runnable {
public String foo = "hello";
public void run() {
foo = "world";
}
}
public static void main(String[] args) {
Thread t = new Thread(new TestRunnable());
t.start();
//How can I get the value of `foo` here?
}
}
最佳答案
我在 java.lang.Thread
中看不到任何方法文档。
那么,我的最佳答案是您可能应该使用 List<Runnable>
代替(或除此之外)List<Thread>
.或者也许你想要某种 map结构,以便您可以从 Thread 访问 Runnable。 (例如,java.util.HashMap<java.lang.Thread, java.lang.Runnable>
)
关于java - 如何通过 Thread 访问 Runnable 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2300579/