• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is the following code valid?  RSS feed

 
Siva Prasad
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • 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)
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • 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.
 
Brent Worden
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • 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.
 
Thomas Smets
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • 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,
 
Jim Crawford
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • 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?
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • 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?
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!