• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

a Thread question

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to clarify something about the following methods...
wait notify notifyAll - have to be in synchronized block/method
sleep join yield - have to be in try block

if they arent, compile error occurs.

am i right?
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some clarifications:
  • wait/notify/notifyAll - Not just any synchronized block or method will do. The currently executing thread must be in a synchronized block that locks on the object that you're invoking the wait/notify/notifyAll on. However, if you don't do that, you won't get a compile-time error; instead, you'll get a runtime exception.
  • yield - This does not throw any checked exceptions, so you don't need to invoke it within a try block.
  • wait/sleep/join - All three of these can throw an InterruptedException, so you'll usually want to invoke them within a try block. As an alternative, you can of course declare that the enclosing method throws an InterruptedException. Either way, if you don't handle the exception, you'll indeed get a compile-time error. Note that you apparently forgot to list wait() among the methods with checked exceptions.
  •  
    Ranch Hand
    Posts: 424
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Also see this thread for a related discussion.
     
    I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic