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).]
SCJP2
Cindy Glass
"The Hood"
Sheriff
Joined: Sep 29, 2000
Posts: 8521
posted
0
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.
"JavaRanch, where the deer and the Certified play" - David O'Meara
atul kashyap
Ranch Hand
Joined: Mar 22, 2001
Posts: 34
posted
0
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
Joined: Mar 17, 2001
Posts: 54
posted
0
thanks more elaboration would definitly do Zaeem
Cindy Glass
"The Hood"
Sheriff
Joined: Sep 29, 2000
Posts: 8521
posted
0
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.