File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread.sleep() does not release lock?

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ]
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)...
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic