File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread -IllegalMonitorStateException and IllegalThreadStateException " Watch "Thread -IllegalMonitorStateException and IllegalThreadStateException " New topic

Thread -IllegalMonitorStateException and IllegalThreadStateException


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.

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.

I agree. Here's the link:
subject: Thread -IllegalMonitorStateException and IllegalThreadStateException
jQuery in Action, 3rd edition