2

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

some_id
  • 29,466
  • 62
  • 182
  • 304

1 Answers1

4

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?
    }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555