• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

IllegalThreadStateException

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Mits implements Runnable {

public static void main ( String args [ ] ) {
Thread tx = new Thread ( new Mits ( ) , " Krux " ) ;
tx . start ( ) ;
System . out . println ( " Terminated " ) ;
}

public void run ( ) {
try
{
Thread . currentThread ( ) . setDaemon ( true ) ;
Thread . sleep ( 10000 ) ;
}
catch (InterruptedException e) { }
}
};
The output is:

Terminated
Exception in thread " Krux " java.lang.IllegalThreadStateException
at java.lang.Thread.setDaemon(Unknown Source)
at Mits.run(Mits.java:12)
at java.lang.Thread.run(Unknown Source)

Why this exception comes???
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
setDaemon() throws IllegalThreadStateException if the thread is active.
 
Swati Kadam
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tim Weide:
setDaemon() throws IllegalThreadStateException if the thread is active.



So can we call setdeamon() after start() method???
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Swati Kadam:
So can we call setdeamon() after start() method???



Here is the relevant excerpt from the JavaDoc....

This method must be called before the thread is started.



Henry
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Swati,

You are making the thread a Daemon thread, Just want to ask you one thing:

Once a thread started, Can you change it properties ? Like its name or something else..!!

I know most probably your answer will be "NO". Similarly setDaemon cause your thread to run as a Daemon thread in the background. So you must set the Daemon property of thread before running. By default it start running like a non-Daemon, now in between its running state we can not make it Daemon.

It will be either fully Daemon or non-Daemon.
reply
    Bookmark Topic Watch Topic
  • New Topic