This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread -IllegalMonitorStateException and IllegalThreadStateException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread -IllegalMonitorStateException and IllegalThreadStateException " Watch "Thread -IllegalMonitorStateException and IllegalThreadStateException " New topic
Author

Thread -IllegalMonitorStateException and IllegalThreadStateException

esunilk
Greenhorn

Joined: Dec 30, 2008
Posts: 1
Hi all,

I am newbie to this forum and preparing for scjp exam.

below code is sample program.




can anyone explain

1. what is IllegalThreadStateException and when we get this exception with any good example.

2. and what is IllegalMonitorStateException with good example.

Many thanks in advance
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Calling 2 times start on a single thread is IllegalThreadStateException.

Calling wait() or notify() on any object without having lock (mean synchronized) on that object is IllegalMonitorStateException.



SCJP 6
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
You have called start method of the thread in a for loop. When a thread calls a start method it will invoke run and ends to complete (assume there is timeslicing). After completion of the run method the thread is not in runnable state. But your for loop is iterating and the thread has already completed its run method and not in runnable state.
This the reason for throwing IllegalThreadStateException

For your second question, the api says
Thrown to indicate that a thread has attempted to wait on an object's monitor or to notify other threads waiting on an object's monitor without owning the specified monitor.



sat
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread -IllegalMonitorStateException and IllegalThreadStateException