• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

K&B Chapter 9 Question # 16

 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic