This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Beginner Question on threads Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply 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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Beginner Question on threads
 
Similar Threads
Help using threads
Help using threads synch
Thread Vs. Runnable
Help using threads synch methods
Does accessing an Object from a synchronized method lock the Object?