• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronization

 
raj l. mohan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 10266
60
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
raj l. mohan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to all, now i understand this progrems.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic