• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Beginner Question on threads

 
Eugene Armistead
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have an Object that gets locked by various threads via a synchronized block. Something Like:
class MyThread extends Thread{
public void run(){
synchronized(someObject){
// do stuff to some Object
}
}
}
Is there an easy way to have an another instance of MyThread examine "someObject" and see if it is locked by another thread? This way I could tell the user something like "waiting for locked resource..."
Thanks
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I tried out the following code and started two threads
which share the same object "MyThread" as shown below :

[This message has been edited by Angela Narain (edited October 04, 2001).]
 
Eugene Armistead
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Angela,
This works perfect for what I need. Thanks for your help!
Regards,
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Angela Narain:
[B]Hi,
I tried out the following code and started two threads
which share the same object "MyThread" as shown below :

[This message has been edited by Angela Narain (edited October 04, 2001).][/B]

This can not work. Once you are into the callMe function, you will never see wait == false. This is because the lock will not be released by the FIRST thread to acquire it, untill the thread is done and leaving the method. The next thread to grab the lock will always be faced with wait == true. Did you ever see the "waiting" message?
You need something slightly different

That should do what you seek. Do you understand the difference here?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic