I'm trying to synchronize a String literal, so that I can control the execution of my threads but for some reason that does not work..
Is there any problem with the way its synchronized?
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);
}
}