File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread.sleep() does not release lock? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread.sleep() does not release lock?" Watch "Thread.sleep() does not release lock?" New topic
Author

Thread.sleep() does not release lock?

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707


Output:

Woke up from sleep
Woke up from sleep
Woke up from sleep
Woke up from sleep
Woke up from sleep
Woke up from sleep
Woke up from sleep
Woke up from sleep
Woke up from sleep
Woke up from sleep
sum = 50

Does this example demonstrate the fact "Thread.sleep(...) does not release the lock"?


Kindly suggest!



Thanks,
cmbhatt
[ April 24, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

Does this example demonstrate the fact "Thread.sleep(...) does not release the lock"?


The Thread.sleep() method does not release any locks. Where did you read/heard that it did?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Thanks Henry,


The Thread.sleep() method does not release any locks. Where did you read/heard that it did?


Nay, I only read/heard the original one "does not release...".

Actually I supposed to see what exactly happens in case if
an object is notified from within a loop where we call to the
sleep(...) method inside the sync block.


Anyways thank for your reply!


Regards,
cmbhatt
swarna dasa
Ranch Hand

Joined: Mar 15, 2007
Posts: 108
I don't get this

I thought the output would be 50.
Though the demothread calls notify it doesn't leave the synchronized context till i=50, so the main thread cannot print the sum, as it occurs in the synchronized context.

What am i failing to understand?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by swarna dasa:
... I thought the output would be 50...

As posted, the output should be 50. I'm guessing this code was altered to get more "manageable" output.

...while(++i<=10)...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread.sleep() does not release lock?