Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Is the following code valid?

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If so, is it safe to do this way?

public class ThTest implements Runnable
{
public static void main(String[] args) throws Exception
{
new ThTest().runThis();

}

public void runThis()
{
Thread m1 = new Thread(this);
Thread m2 = new Thread(this);
Thread m3 = new Thread(this);
m1.start();
m2.start();
m3.start();
}

public void run()
{
System.out.println("It's me ");
notify();
}
public void myFun() throws Exception
{
wait();
}
}

I get the following when I run this:
It's me
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.notify(Native Method)
at ThTest.run(ThTest.java:25)
at java.lang.Thread.run(Thread.java:479)
It's me
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.notify(Native Method)
at ThTest.run(ThTest.java:25)
at java.lang.Thread.run(Thread.java:479)
It's me
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.notify(Native Method)
at ThTest.run(ThTest.java:25)
at java.lang.Thread.run(Thread.java:479)
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if you're gettting exceptions, the easy answer is "no". Your code is not valid. :roll:
You should read the JavaDoc for notify(). It requires that the calling thread owns the monitor on the object you're calling notify() from. In case you didn't know that, the Java VM is being kind enough to remind you with the exception:
java.lang.IllegalMonitorStateException: current thread not owner
The easiest way to fix this is to use the syncrhonized keyword in run's method declaration. That's not usually the best method to synchronize, but it will fix your simple example application.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To call notify or wait on an object, the current thread must be the owner of the object's monitor.
The easiest way to become the monitor owner is to synchronize on the object.
In your case:

Check out the javadoc for notify and wait at http://java.sun.com/j2se/1.3/docs/api/java/lang/Object.html for further clarification.
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What bothers me with you code is that you create one & pass it as construtor argument to three ?
I can hardly imagine a case where that would be needed !
But to nicely crash the VM !
Thomas,
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thomas SMETS:
What bothers me with you code is that you create one & pass it as construtor argument to three ?
I can hardly imagine a case where that would be needed !
But to nicely crash the VM !
Thomas,



I believe the idea was to create three threads, but this wouldn't work, right? What would the effect actually be?
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Starting three threads with a single Runnable is not generally a good idea, but why would it crash the JVM?
 
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
It is not valid because you are attempting to start the same object 3 times by 3 different Threads. The 2nd time to try to start it, it will be in an invalid state to be started. That will give you an error.
 
We're being followed by intergalactic spies! Quick! Take this tiny ad!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic