This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Threads and Synchronization and the fly likes Beginner Question on threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Beginner Question on threads" Watch "Beginner Question on threads" New topic
Author

Beginner Question on threads

Eugene Armistead
Greenhorn

Joined: Jul 12, 2001
Posts: 10
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

Joined: Apr 14, 2001
Posts: 327
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

Joined: Jul 12, 2001
Posts: 10
Hi Angela,
This works perfect for what I need. Thanks for your help!
Regards,
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Beginner Question on threads