• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Beginner Question on threads

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Angela,
This works perfect for what I need. Thanks for your help!
Regards,
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Hey! You're stepping on my hand! Help me tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic