我正在尝试同步一个字符串文字,以便我可以控制我的线程的执行,但由于某种原因它不起作用..
它的同步方式有什么问题吗?
package scratch1;
public class OrderedThread {
public static void main(String[] args){
RunThread first, second, third;
OrderedThread orderedthread = new OrderedThread();
first = new RunThread("One",orderedthread);
second = new RunThread("Two",orderedthread);
third = new RunThread("Three",orderedthread);
second.start();
first.start();
third.start();
}
public void display(String msg){
synchronized(msg){
for (int i = 1; i <= 20; i++){
System.out.println("Name = "+ msg);
}
}
}
}
class RunThread extends Thread{
String name;
OrderedThread orderT;
RunThread(String name, OrderedThread orderT){
this.name = name;
this.orderT = orderT;
}
public void run(){
orderT.display(name);
}
}