aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread java.lang.IllegalMonitorStateException: Iam new to threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread java.lang.IllegalMonitorStateException: Iam new to threads" Watch "Thread java.lang.IllegalMonitorStateException: Iam new to threads" New topic
Author

Thread java.lang.IllegalMonitorStateException: Iam new to threads

Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
I want to write a program which prints "hello world" n number of times. Actually, i want to use two threads, one prints "hello" and the
other prints "world".

now , i thought thread1(say which prints "Hello") will print "Hello" and then call wait(),Then thread2 will print "world" and call notify()
Please find below the code i though of:










The output i get is
Hello
World

Exception in thread "Thread-2" java.lang.IllegalMonitorStateException
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at java.lang.Object.wait(Native Method)
at Thread2.printWorld(Thread2.java:28)
at java.lang.Object.wait(Object.java:199)
at Thread2.run(Thread2.java:17)
at Thread1.printHello(Thread1.java:30)
at Thread1.run(Thread1.java:16)
at java.lang.Thread.run(Thread.java:803)
at java.lang.Thread.run(Thread.java:803)


Could anyone please help how can i get the desired output?

Man Suraj
Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

In both the codes you are synchronizing on q object and calling wait/notify on this object. Chance your code to call q.wait and q.notify...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

As Ankit says, your code calls the wait() of "this" (the current object) instead of q. You synchronized over q so you should wait over q.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
Thanks guys :-)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

Short answer: wait() and notifiy() requires that you synchronize on the object that you are waiting on. In your example, if you do a wait() and notify() on the same object that you used to lock the sync block, then you will get rid of the illegal state exceptions.


Long (and more complex) answer: There is a very good reason why the wait() needs the lock -- there is an unsolvable race condition, unless the wait() method releases the lock as part of its waiting process. With this race condition, it is possible for notifications to be lost.

In your example, you too, will need to understand why -- because you are also releasing the lock, independently of wait, (and also doing blind waits), which can lead to losing notifications (and making unneeded wait() method calls).

Henry



Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
Thanks Henry....

But my above code still doesnot work the way I want it to run. As suggested in earlier posts, I have changed my code
to "q.notify()" instead of "notify()" in Thread2.java and "q.wait()" instead of "wait()" in Thread1.java. Now I dont get any exception , but output is not desired one. I got different outputs different times (as we expect with threads).

The output i got this time is
hello
Thread1@2e022e02
0
world
Thread2@31303130
0
world
Thread2@31303130
0
hello
Thread1@2e022e02
0
world
Thread2@31303130
0
world
Thread2@31303130
0
world

Basically , i wanted to see "Hello" then "World". But in my ouput I see hello, then 2 times world (Kept in bold for clarity) and this goes on....Please let me know wht the problem is
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
still there will be a few problems in the code it will compile and run but will not do what you want moreover it is a never ending program.
try and improve it.


SCJP 1.6 96%
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

Maan Suraj wrote:
Basically , i wanted to see "Hello" then "World". But in my ouput I see hello, then 2 times world (Kept in bold for clarity) and this goes on....Please let me know wht the problem is


You actually have more than one issue... which I have explained in my previous post.

But here is one of the easy ones... The "world" threads does it operation in a blind way. It prints worlds, sends the notification, and assumes that the other thread finished printing "hello". This assumption is bad. Why? Even if the other thread received the notification (and you have other issues here), it needs to get a timeslice, and have the time to print "hello". Shouldn't your "world" thread wait and then confirm, that the "hello" is finished before printing the next "world"?

Henry
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
you make the tread1 to wait till thread2 prints world but dont make it wait till thread1 again prints hello, so after getting notified the thread1 prints hello but thread2 continues to execute and print world in between so after notifying thread1 put thread2 to wait.
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
Got it :-) ................Thanks Neha, Henry and everyone else who answered to my question.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread java.lang.IllegalMonitorStateException: Iam new to threads