我想启动一个线程,并传入一个我在某个地方创建的对象,但想要在线程中设置其值。
如何实现的?
谢谢
最佳答案
构造Thread
(最好是Runnable
)时,只需传递它即可:
public class Task implements Runnable {
private YourObject yourObject;
public Task(YourObject yourObject) {
this.yourObject = yourObject;
}
@Override
public void run() {
yourObject.setSomething("something"); // See?
}
}
关于java - 用参数启动线程,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4394295/