Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

diff b/w timedWait() and wait(millisecs)  RSS feed

 
Vinney Shanmugam
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
both are going to wait for a stipulated time and comes out of it,if they are not notified while waiting.
what is the difference between them?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The differences are minor, and clear from the API. wait(millis) is defined in the Object class, while timedWait(obj, delay) is in TimeUnit. The only benefit to TimedUnit is it may be slightly more convenient (with fewer visible calculations) for longer waits. E.g for 3 hours, these are equivalent:

 
Don Solomon
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
neither one of these wait methods will come out of the WAITING state, regardless of what time you specify, until they take ownership of the monitor.
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Don Solomon:
neither one of these wait methods will come out of the WAITING state, regardless of what time you specify, until they take ownership of the monitor.


Another way of coming out of waiting state is when the timed wait is over and the wait is interrupted.
 
Don Solomon
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Satya Maheshwari:


Another way of coming out of waiting state is when the timed wait is over and the wait is interrupted.



i somewhat agree. a thread can be interrupted anytime regardless of the status of a timed wait. but a thread will not come out of a wait, timed or not, until it has ownership of the monitor - unless it is interrupted.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Don]: but a thread will not come out of a wait, timed or not, until it has ownership of the monitor - unless it is interrupted.

Even if it's interrupted, it will need to reacquire ownership of the monitor before it can resume execution.
 
Don Solomon
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes mike, i stand corrected. you are right.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!