• Post Reply Bookmark Topic Watch Topic
  • New Topic

Keeping the Lock  RSS feed

 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I was just wondering, does it make sense to call sleep,join,yield methods on a Thread in a synchronized block? Coz since all the three methods when called on a thread, the thread keeps the lock, I think it doesn't make any sense. Am I correct?

Thanks.
 
Henry Wong
author
Sheriff
Posts: 22853
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, "it doesn't make any sense" is a bit overboard. Maybe "inefficient" is a better way to put it.

It is possible to have thread unsafe portions of code, that include waiting periods. I can make a request for a service, wait for the result, make another request, etc., and have some state between requests, that releasing a lock may not be possible.

Henry
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okie.. Thanks Henry for the reply.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes you want to hold the lock a while. Even if for testing purposes.
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Um... if possible can someone give a code example?

Thanks.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes you want to make sure your logic is correct. That as long as the lock is held, for instance, no new files will be created. So you put a sleep statement just after you get the lock, just to test out how your program will behave when no other threads are allowed to get the lock.

Its good for testing.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!