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
  • 28,896
  • 60
  • 180
  • 299

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,040,783
  • 362
  • 3,548
  • 3,513