• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Is Thread.sleep() synchronised method ?

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,



I create two thread instances of ThreadTest and call the start method of the thread.

In this case whether the Thread.sleep() is synchronised or not ?
Does the user has to handle the synchronised part ?

Regards
Atul
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, sleep() is not a synchronized method, and has no special synchronization concerns -- other than the fact that calling sleep() while holding the lock of any object may be a bad idea.
 
Atul Prabhu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks Ernest Friedman-Hill.

If one wants that the current thread to sleep() what should be done without acquiring the lock on the shared object ?

would a call to function which just iterates the "for" loop be sufficient ?
i.e for(int i =0 ; i <1000; i++) {}

or is there anything else which I am not aware of.

Regards
Atul
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using loops to implement time delays went out with BASIC. It is a bad idea because the amount of time delay may vary hugely depending on processor power, loading and also on the extent to which compiler and/or hotspot are able to optimise-away redundant code like an empty loop.

Use Thread.sleep() or Thread.wait(). The former does not require you to hold the lock on "this" and indeed you generally should not hold any locks when doing sleep(). The latter does require you to hold the lock on "this", but releases it while wait()ing, then regains it when finished waiting.
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Peter Chase:


Use Thread.sleep() or Thread.wait(). The former does not require you to hold the lock on "this" and indeed you generally should not hold any locks when doing sleep(). The latter does require you to hold the lock on "this", but releases it while wait()ing, then regains it when finished waiting.



wait() a an instance method of Object (not only Thread). ("this" is the object used to call wait().)
 
reply
    Bookmark Topic Watch Topic
  • New Topic