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


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

Synchronization

raj l. mohan
Greenhorn

Joined: Nov 22, 2012
Posts: 20

output:
A Thread-0 this refers->ExerciseLock
A Thread-0 this refers->ExerciseLock
A Thread-0 this refers->ExerciseLock
A Thread-0 this refers->ExerciseLock
A Thread-0 this refers->ExerciseLock
A Thread-0 this refers->ExerciseLock
A Thread-0 this refers->ExerciseLock
A Thread-0 this refers->ExerciseLock
A Thread-0 this refers->ExerciseLock
A Thread-0 this refers->ExerciseLock
B Thread-2 this refers->ExerciseLock
B Thread-2 this refers->ExerciseLock
B Thread-2 this refers->ExerciseLock
B Thread-2 this refers->ExerciseLock
B Thread-2 this refers->ExerciseLock
B Thread-2 this refers->ExerciseLock
B Thread-2 this refers->ExerciseLock
B Thread-2 this refers->ExerciseLock
B Thread-2 this refers->ExerciseLock
B Thread-2 this refers->ExerciseLock
C Thread-1 this refers->ExerciseLock
C Thread-1 this refers->ExerciseLock
C Thread-1 this refers->ExerciseLock
C Thread-1 this refers->ExerciseLock
C Thread-1 this refers->ExerciseLock
C Thread-1 this refers->ExerciseLock
C Thread-1 this refers->ExerciseLock
C Thread-1 this refers->ExerciseLock
C Thread-1 this refers->ExerciseLock
C Thread-1 this refers->ExerciseLock


output:
A Thread-1 this refers->ExerciseLock
A Thread-1 this refers->ExerciseLock
A Thread-2 this refers->ExerciseLock
A Thread-2 this refers->ExerciseLock
A Thread-2 this refers->ExerciseLock
A Thread-2 this refers->ExerciseLock
A Thread-2 this refers->ExerciseLock
A Thread-2 this refers->ExerciseLock
A Thread-2 this refers->ExerciseLock
A Thread-0 this refers->ExerciseLock
A Thread-0 this refers->ExerciseLock
A Thread-0 this refers->ExerciseLock
A Thread-0 this refers->ExerciseLock
A Thread-0 this refers->ExerciseLock
A Thread-0 this refers->ExerciseLock
A Thread-0 this refers->ExerciseLock
A Thread-2 this refers->ExerciseLock
A Thread-2 this refers->ExerciseLock
A Thread-2 this refers->ExerciseLock
A Thread-1 this refers->ExerciseLock
B Thread-1 this refers->ExerciseLock
B Thread-1 this refers->ExerciseLock
B Thread-1 this refers->ExerciseLock
B Thread-1 this refers->ExerciseLock
A Thread-0 this refers->ExerciseLock
B Thread-1 this refers->ExerciseLock
B Thread-1 this refers->ExerciseLock
B Thread-0 this refers->ExerciseLock
B Thread-0 this refers->ExerciseLock
B Thread-1 this refers->ExerciseLock


I'm using this keyword in both programs for synchronized blocks but why does synchronization work in first program but not in second program ?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

raj mohan lund wrote:I'm using this keyword in both programs for synchronized blocks but why does synchronization work in first program but not in second program ?

Well, it really depends what you mean by "work", but the reason they behave differently is that in the first case you only have one Runnable object and in the second you have three, so in the first case the loop must complete before the next Thread can start it. In the the second case, synchronization would appear to be redundant.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14272
    
  21

Because in line 12 you synchronize on the ExerciseLock object that the method is called on, and in the second example you are creating three different ExerciseLock objects (lines 23, 24, 25). Each thread synchronizes on a different object.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
raj l. mohan
Greenhorn

Joined: Nov 22, 2012
Posts: 20
thanks to all, now i understand this progrems.
 
Consider Paul's rocket mass heater.
 
subject: Synchronization