aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread waiting forever, Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread waiting forever, " Watch "Thread waiting forever, " New topic
Author

Thread waiting forever,

Rohit Men
Greenhorn

Joined: Aug 30, 2005
Posts: 9
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

Joined: Oct 25, 2000
Posts: 7292

For me it printed 1 but did not terminate.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Here, play with this instead:
Roja Rani
Greenhorn

Joined: Sep 28, 2005
Posts: 22
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

Joined: Oct 25, 2000
Posts: 7292

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

Joined: Sep 27, 2005
Posts: 36
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

Joined: Aug 28, 2005
Posts: 340
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 ]

Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

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

Joined: Oct 02, 2005
Posts: 31
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 ]

SCJP 1.4 (96%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread waiting forever,