• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B Chapter 9 Question # 16

 
Paras Jain
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic