Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Volatile use with Synchronization

 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i am running below code using volatile keyword but it gives result unexpected. it is working on two threads but not with the three.

if i am using two thread then it give output something like this:
Thread 1 : 1
Thread 2 : 1
Thread 1 : 2
Thread 2 : 2
Thread 1 : 3
Thread 2 : 3
Thread 1 : 4
Thread 2 : 4
Thread 1 : 5
Thread 2 : 5
............more

please tell me why this is not working in three thread?


 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should always wait() in a loop that test's if the wait condition is satisfied.
The two thread version is equally broken, by the way, because a thread can also wake up without being notified, which is called spurious wakeup.
 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you tell me from some other way
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic