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:
MyThread x = new MyThread();
new Thread(x).start();
new Thread(x).start();
new Thread(x).start();
Now there will be three threads all running code in the same object.
I suggest you rename MyThread
as it's not a thread - it's a task for a thread to perform. That makes it clearer (IMO).
关于并发的Really Big Index跟踪非常值得一读(是的,它有示例)。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.