• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dan's thread  RSS feed

 
Robbie kyodo
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How come ABC is printed ? I took out the synchroized and it still prints ABC.
class At extends Thread {
String[] sa;
public At(String[] sa) {
this.sa = sa;
}
public void run() {
synchronized (sa) {
System.out.print(sa[0] + sa[1] + sa[2]);
}
}
}
public class Bt {
private static String[] sa = new String[]{"X","Y","Z"};
public static void main (String[] args) {
synchronized (sa) {
Thread t1 = new At(sa);
t1.start();
sa[0] = "A";
sa[1] = "B";
sa[2] = "C";
}
}
}
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Robbie
How come ABC is printed ? I took out the synchroized and it still prints ABC.

Well I guess the question is trying to ask you what is the output of the program in any case. The output of program with synchronized code is gauranteed to be ABC. But when you remove the synchronized code it may or may not be ABC. It may differ from time to time or just may not. But without the synchronized block ABC is not gauranteed. Try the following two code. I have added a sleep method so that it becomes easy to see whats really happening.

Now remove both the synchronized blocks and keep the sleep method and you'll see the difference. Here is the code without the synchronized blocks.

Hope that helps.
 
Rory French
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Robbie. I agree with Anupam. Anupam, you beat me to the answer, but here's my take on the question anyway:
As the program stands, in the main method, the following happens:
- The sa array is locked by the main thread.
- the thread t1 is created and started by the main thread. As long as the main thread has a lock on the sa array, the body of the run method in class At(which will be invoked as a result of starting the t1 thread) cannot proceed (i.e. cannot enter the synchronized(sa) block) until the main thread releases its lock on sa.
- Before the main thread releases its lock on sa (i.e. before it exits its synchronized block), the main thread changes the contents of sa (to "A", "B", "C").
- Once the main thread releases its lock on sa the t1 thread will no longer be blocked and will be free to aquire the lock on sa. The t1 thread will then print the new/changed values (i.e. "A", "B", "C") of sa.

If you remove the synchronized statement from the main method, then the output of the program is theoretically unpredicatable: the main thread could change the values of sa (to "A", "B", "C") before the t1 thread starts, OR t1 could run immediately after the call to t1.start(), aquire the lock on sa, and print "XYZ".
[ May 18, 2003: Message edited by: Rory French ]
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are not there two sa objects one in each class and each synchronize is locking on two distinct objects?
 
G Nadeem
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barkat,
No. (As evidenced from above explanations) There is only String[] object (sa) being created in Bt class's main method and same is passed as argument to the constructor of class At. so reference is to same object.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!