File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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
 
Similar Threads
Question on synchronized methods
ExamLab Exception
Examlab(SCJP 6 Practice exam 3) Thread Question
Working on an Object with two threads of another class.
Question on lock