Reputation: 29896
I would like to start a thread and pass in an object which I create somewhere but want to set its values from within the thread.
How is this achieved?
Thanks
Upvotes: 2
Views: 146
Reputation: 1108712
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?
}
}
Upvotes: 4