File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B Chapter 9 Question # 16 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B Chapter 9 Question # 16" Watch "K&B Chapter 9 Question # 16" New topic
Author

K&B Chapter 9 Question # 16

Paras Jain
Ranch Hand

Joined: Feb 26, 2005
Posts: 137
As suggested in the book
I have modified the method by synchrozing the method call. I end up in getting infinite loop. What is wrong?
When I write wait(1000) instead of wait() then also nothing gets printed



Paras Jain
SCJP 5.0
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi, Paras,

not a problem of synchronization alone.

In your method, nothing is printed if(x<10)
And x is always five. You never get to the else{} block.

As your whole method block is synchronized to this, I made the code a bit easier to read by defining the whole method as sync.

And I put some output lines at the end of doStuff()





Without the 1234 it will wait for ever. Or up to a spontanous wakeup.
Also here, x is never printed.

Yours,
Bu.


all events occur in real time
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023

I have modified the method by synchrozing the method call. I end up in getting infinite loop. What is wrong?
When I write wait(1000) instead of wait() then also nothing gets printed


By code design.
Case 1: x always less than 10 and threads call wait() and no other threads to wake up them.

Case 2: threads call wait(1000) and will wake up after 1000ms. and returns from run(). The thread ends.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B Chapter 9 Question # 16
 
Similar Threads
book K&B self test q16........problem
Question about wait() and notify()
On threads
using wait() method in Threads
runtime exception with respective to thread program