Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread waiting forever,

 
Rohit Men
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This is sample code from the Dan's mock exam july21-chap17-exam2-question13

The author says that it will print 0 or above. When I run it on windows it don't print anything and waits forever.
Even I think that we can't say for surity that it will print 0.

Please explain.
[ October 01, 2005: Message edited by: Michael Ernest ]
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For me it printed 1 but did not terminate.
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here, play with this instead:
 
Roja Rani
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The above program is running fine and terminating correctly. Without using notify how it is executing the while condition again?

i.e, when it first time comes to run method, done is false thus it goes to wait state. when it is notified to check the while condition?

please clarify.
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The dynamic you're thinking of here applies to the suspend/resume scheme that was once available in Java. In that scheme, a suspended thread could not continue unless it was resumed by another thread or by the system.

The wait/notify scheme in Java closely models what is called a conditional variable lock in multithreaded programming. Suffice it to say here that if a thread waits and there is no one to notify it, the system will eventually allow the thread to run again to see if the condition (!done) has been satisfied.

The full explanation is complex; I would need a bit of time to figure a short way to explain it.
 
Alpana Singh
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rohit,

When I ran your code it prints 1128230332609 and waited.

I don't know the reason behind this.I am just waiting for the answers
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But since we cannot be sure that threads would produce same output on different machines.....how could we know the precise answer.
Rohit your code for me printed 0 and waited infinitely.
[ October 02, 2005: Message edited by: Sandeep Chhabra ]
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no exact answer; results will vary.

I understood the mock question to say the code should print zero or more, which makes perfect sense. With the code given, there is no way for the 'finish' time to be less than the 'start' time.

Since time is measured in milliseconds for this code, it's possible that the two times will be taken less than a millisecond apart, hence the possible value of zero.
 
Sid Remey
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Here is a slightly modified version that actually calls notify to wake the sleeping thread.



I also synchronized access to the 'done' variable as per Effective Java Item #48; otherwise thread a1 may never see the update from thread main if you are running on a multi-cpu system.
[ October 02, 2005: Message edited by: Sid Remey ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic