Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Threading and synchronization  RSS feed

 
Ken Teoh
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I have a quiz question I hope you guys can understand



public class OrderedThread {
public static void main(String[] args) {
MBThread first,second,third;
OrderedThread orderedThread = new OrderedThread();
first = new MBThread("One",orderedThread);
second = new MBThread("Two",orderedThread);
third = new MBThread("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 MBThread extends Thread {
String name;
OrderedThread orderT;

MBThread(String name, OrderedThread orderT) {
this.name = name;
this.orderT = orderT;
}

public void run () {
orderT.display(name);
}
}


The solution state that two,one and three will be printed in an indefinite order. Do you understand why is this the case? Also, I ran the code in eclipse and it was printing two,one three 20 times each in order. Lastly is there a way to print it in order? Thanks
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a good exercise. Exploration and experimentation like this is great, so I'll try not to give you too much in the way of answers. When I ran this from the command line the names One Two and Three were nicely scrambled.

The synchronized block assures that only one thread can get the monitor of some object at a time. Are all three of your threads trying to get the monitor of the same "name" object? If not, what effect does synchronize on three different objects have?

Try changing the synchronized block to sync on (this). Then all three threads will sync on the same object for sure. See what that does to the scrambling. This illustrates a problem with a long-running synchronized block because the threads don't get much chance to interleave.

If you could get this tuned perfectly so it prints One 20 times then Two 20 times then Three 20 times, what would be the benefit of using threads?
 
Promod kumar
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To see indefinite, scrambled results, I changed the for loop to



As far as what is going on in the code, I am still trying to figure it out.
 
Promod kumar
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a couple of variations on how to have them print not scrambled(although you might want to think about Stan's question about the purpose of threads etc).

In the first approach, I am sending in the same String "one" to all the constructors.
Let me know if this helps or if you need further explanation.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!