Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Aquiring lock on objects  RSS feed

 
Montano Mazvik
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am using jdk1.4, I got a problem in following peice of code.

My requirement is that 'refreshAll' function should be executed by one thread/request at a time AND meanwhile if any other thread/request attempts to run it then user should be prompted with error message that "operation already being performed by other user".

if(operation.equals("refreshAll")){
synchronized(this){
refreshAll(scope);
}
}

Please let me know how can I find out if a thread is already locked ?

Thanks
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using the synchronization keyword, you can not check whether the lock is already acquired.
One way is to use the the backport of JSR 166. This utility has the implementation of Reentrant locks with an option of tryLock() which is exactly what you want.

The other option can be to implement something like a semaphore with a single permit.
 
Montano Mazvik
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Nitesh, backport solved my problem

Cheers.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!