• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Thread -IllegalMonitorStateException and IllegalThreadStateException

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic