Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Issue with synchronization  RSS feed

 
David Spades
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


first of all, is there any chance that this program will ouput the line "A Hello!"? I always got "B Hello!".
if it'll never print "A Hello!", could anybody explain why?
thanks
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Spades wrote:
first of all, is there any chance that this program will ouput the line "A Hello!"? I always got "B Hello!".
if it'll never print "A Hello!", could anybody explain why?


Scheduling of threads is not defined by the JVM specification. This is mainly to allow the native threading system to schedule threads as they like. This is also true with the use of condition variables (ie. wait/notify).

So, to answer your question... The behavior is not defined.

Henry
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have just added a while loop and see that both B Hello and A Hello do appear but not in any deterministic order.



This should be normal.
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will be good to know what are you trying to prove/test here.
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Spades wrote:
first of all, is there any chance that this program will ouput the line "A Hello!"? I always got "B Hello!".
if it'll never print "A Hello!", could anybody explain why?
thanks


David ,
When I'm running your code am getting below output :

List is empty...
List is empty...
Opening...
Interrupted Exception!
New Element:'Hello!'
notifyAll called!
Closing...
Interrupted Exception!


Your code it is quite self explanatory ..

If I modify the code like this


then I get :

List is empty...
Interrupted Exception!
Opening...
New Element:'Hello!'
notifyAll called!
Closing...
A Hello!

Thanks ,
Satya
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!