• 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

Threads & Locks??

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Friends,
AM studying for SCJP & in some mocks I came across the following question.
true or false.
Q. sleep() releases the lock before going to sleep?
1. true.
2. false.

I selected false(it does not release locks) But the mocks say true(It releases it??)
How come??.
Can anybody explain.
Thanks.
Jiger Patel.
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jiger,
You are correct and the mock exam is wrong.
I wrote a code to prove that sleep() does not release the lock. It instantiates two thread. Each thread will enter a synchronized method (requiring a lock on the object), and will be put to sleep for 5 seconds. If the lock is release, then the second thread should be able to execute the same method almost immediately. The output shows that this is not the case.

Opps...Forgot the output:
Going to sleep for 5000 msecs. Thread-0 981870062930
Waking up from sleep: Thread-0 981870068030
*****************************************************
Going to sleep for 5000 msecs. Thread-1 981870068030
Waking up from sleep: Thread-1 981870073030
*****************************************************
Notice that Thread-0 sleeps and wakes up before Thread-1 gets a chance to enter the synchronized method.
-Peter
[This message has been edited by Peter Tran (edited February 10, 2001).]
[This message has been edited by Peter Tran (edited February 10, 2001).]
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jiger,
It's pretty easy to modify the code to test if yield() releases the lock. With yield, it's a little more tricky. I added a bunch of yield() statement to simulate some long execution of code. Within all those yield() statements, you would think if the lock was freed up, the second thread would get a chance to run. Just to hedge my bet, I also gave the second thread a much higher priority than the first thread. If the lock is release by the first thread (Thread-0), then the second thread (Thread-1) should have ran since it has a higer priority. This is not the case.
Here's the modified code.

Here's the output:
Beg yield: Thread-0 981870976120
End yield: Thread-0 981870976230
*****************************************************
Beg yield: Thread-1 981870976230
End yield: Thread-1 981870976280
*****************************************************
Notice that 110 msecs have elapsed from the time Thread-0 begins to execute all the yield() statements to the end. 120 msecs is more than enough time for the second thread (Thread-1) to run, but it wasn't able to because the lock was never freed up by the first thread.
-Peter
[This message has been edited by Peter Tran (edited February 10, 2001).]
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jiger,
And last but not least, the wait() method. Here, I modified the code to use wait() with a timeout of 5000 msecs. If I didn't use the timeout version, my program would have hanged.

And the output:
Beg wait: Thread-0 981871333960
Beg wait: Thread-1 981871333960
End wait: Thread-0 981871338960
*****************************************************
End wait: Thread-1 981871338960
*****************************************************
Notice, that Thread-0 and Thread-1 both start immediately. And both end immediately. My machine is very fast, so 1 msec isn't small enough to measure the how long it takes to acquire and release the object's lock.
In conclusion, sleep() and yield() does not release their locks while wait() does release the lock.
-Peter
[This message has been edited by Peter Tran (edited February 10, 2001).]
reply
    Bookmark Topic Watch Topic
  • New Topic