开发者

Starting a thread with a paramenter

开发者 https://www.devze.com 2023-01-29 08:40 出处:网络
I would like to start a thread and pass in an object which I create somewhere bu开发者_开发技巧t want to set its values from within the thread.

I would like to start a thread and pass in an object which I create somewhere bu开发者_开发技巧t want to set its values from within the thread.

How is this achieved?

Thanks


Just pass it when you construct the Thread (or preferably, Runnable):

public class Task implements Runnable {
    private YourObject yourObject;

    public Task(YourObject yourObject) {
        this.yourObject = yourObject;
    }

    @Override
    public void run() {
        yourObject.setSomething("something"); // See?
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消