Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread java.lang.IllegalMonitorStateException: Iam new to threads

 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys :-)
 
Henry Wong
author
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it :-) ................Thanks Neha, Henry and everyone else who answered to my question.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic