1.

Can we start a thread twice in Java?

Answer»

It is not possible to START a thread twice in Java. In other words, after a thread is started once, it cannot be started again. If the thread is started a second time, then the  IllegalThreadStateException is thrown.

A program that DEMONSTRATES this is given as FOLLOWS:

PUBLIC class ThreadDemo extends Thread {      public VOID run()    {        System.out.println("Thread is running");      }    public static void main(String args[])    {        ThreadDemo thread = new ThreadDemo();          thread.start();          thread.start();      } }  

The output of the above program is as follows:

Thread is running Exception in thread "main" java.lang.IllegalThreadStateException at java.lang.Thread.start(Thread.java:708) at ThreadDemo.main(ThreadDemo.java:13)

As is seen from the above program, if the thread is started the second time, the  IllegalThreadStateException is thrown.



Discussion

No Comment Found