Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Thread.sleep() and lock

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello.can anyone help me with this code? it seems like thread does release lock when it enters to sleep mode when i uncomment the try-catch block.
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Suleyman Suleymanov wrote:hello.can anyone help me with this code? it seems like thread does release lock when it enters to sleep mode when i uncomment the try-catch block.



That's correct. The Thread.sleep() method doesn't release any locks. Were you under the impression that it does? Perhaps you are confusing the sleep() method with the wait() method?

Henry
 
Suleyman Suleymanov
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when i uncomment the try-catch block i get this output :
0 t1
0 t2
1 t1
1 t2
2 t1
2 t2
3 t1
3 t2
4 t2
4 t1
5 t2
5 t1
6 t2
6 t1
7 t1
7 t2
8 t2
8 t1
9 t1
9 t2
doesn't it mean that t1 release lock when it goes to sleep and t2 get the lock?
 
Henry Wong
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Suleyman Suleymanov wrote:when i uncomment the try-catch block i get this output :
0 t1
0 t2

doesn't it mean that t1 release lock when it goes to sleep and t2 get the lock?



Oh... I am sorry. I misread your question...

No. Thread.sleep() does *not* release any locks. The reason that the two threads are interlacing is because the two threads are using two different locks.

Henry
 
Suleyman Suleymanov
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks.i got it.
 
bacon. tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic