some_id
some_id

Reputation: 29896

Starting a thread with a paramenter

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

Answers (1)

BalusC
BalusC

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

Related Questions