aspose file tools*
The moose likes Beginning Java and the fly likes synch in threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "synch in threads" Watch "synch in threads" New topic
Author

synch in threads

zaeem masood ashar
Ranch Hand

Joined: Mar 17, 2001
Posts: 54
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
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
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
thanks
more elaboration would definitly do
Zaeem
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synch in threads
 
Similar Threads
explanation needed
Feed the Pets
Examlab(SCJP 6 Practice exam 3) Thread Question
synchronized test
Error printing on Console !!!!!