Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread.sleep() does not release lock?

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 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: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 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
  • 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
  • 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
  • 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)...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic