I am just playing around with threads in java. I have a class which implements runnable.
public class MyThread implements Runnable{
private boolean finished;
//Other variables
public void run(){
//Thread code
}
}
My understanding is that each thread of type MyThread will have its own copy of member variables and writes to those member variables need not be synchronized. Is this assumption correct? If correct, access to what needs to be synchronized? Can someone care to give a outline or pseudo code.? Thanks.
Not necessarily. You could create multiple threads using the same instance of
MyThread. For example:Now there will be three threads all running code in the same object.
I suggest you rename
MyThreadas it’s not a thread – it’s a task for a thread to perform. That makes it clearer (IMO).