• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synch in threads

 
zaeem masood ashar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
By adding the "synchronized" keyword, the output is not synchronized i.e. "Logic" should always get printed as one word as lock on the object should ensure that the entire block is entertained before any other thread makes an attempt to enter
help needed.......


[This message has been edited by Cindy Glass (edited May 01, 2001).]
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you have 4 separate objects, each with it's own separate lock (you lock an object - not a class). Just because ONE of them is locked, does not prevent the other 3 from running indepentantly in their own threads at the same time.
 
atul kashyap
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cindy can you please explain what actually synchronization does in this example or in general.
Thanks in advance
[This message has been edited by atul kashyap (edited May 02, 2001).]
 
zaeem masood ashar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
more elaboration would definitly do
Zaeem
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took the liberty of reworking your code so that the synchronized code is in an object other than the one that has the thread. Now I have 1 object whose class has a synchronized method and 4 threads that all try to use the one object at the same time. Each one will complete before the next begins.

if you remove the synchronized keyword it comes out scrambled. If you create each Thr with different myTests then it comes out scrambled.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic