Can we call the run() method twice?

When execution of codes inside run() method is completed, the thread finished its task and considered to be dead. Once a thread becomes dead it can not go to any other state. It can not be started again.
If you have a reference to a Thread, and you call start(), it's started. If you call
start() a second time, it will cause an exception (an IllegalThreadStateException,
which is a kind of RuntimeException).
Only a new thread can be started, and then only once. A runnable
thread or a dead thread cannot be restarted.
class demo1 extends Thread
{
public void run(){System.out.println("hi");}
public static void main(String[] args){
demo1 d=new demo1();
d.start();
d.start();
}}
This program at runtime will throw IllegalThreadStateException

No comments:

Post a Comment