in Technology by
Can we start a thread twice in Java?

1 Answer

0 votes
by

No. After starting a thread, it can never be started again. If you does so, an IllegalThreadStateException is thrown. In such case, thread will run once but for second time, it will throw exception.

Let's understand it by the example given below:

  1. public class TestThreadTwice1 extends Thread{  
  2.  public void run(){  
  3.    System.out.println("running...");  
  4.  }  
  5.  public static void main(String args[]){  
  6.   TestThreadTwice1 t1=new TestThreadTwice1();  
  7.   t1.start();  
  8.   t1.start();  
  9.  }  
  10. }  

Output:

running
Exception in thread "main" java.lang.IllegalThreadStateException

Related questions

0 votes
    Which of these method is used to start a server thread? (a) run() (b) start() (c) runThread( ... java programming questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    How can we start using Jenkins from the command line?...
asked Oct 1, 2021 in Technology by JackTerrance
0 votes
    Can we call the run() method instead of start()?...
asked Jan 19, 2021 in Technology by JackTerrance
0 votes
    Should we make the resources thread safe explicitly if they are made to share across multiple clients?...
asked Jun 23, 2021 in Technology by JackTerrance
0 votes
    How do we define the term Thread? (a) Device that controls input (b) Variable that controls movement ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 22, 2021 in Education by JackTerrance
0 votes
    Thread priority in Java is? (a) Integer (b) Float (c) double (d) long The question was asked in ... in chapter Multithreading of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which feature of java 8 enables us to create a work stealing thread pool using all available processors at ... Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    I am using simple thread to execute the httpGet to server when a button is clicked, but I get ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    Which of the interface contains all the methods used for handling thread related operations in Java? (a) ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    Thread priority in Java is? (a) Integer (b) Float (c) double (d) long The question was asked in ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    Can we execute any code before main method in Java?...
asked May 18, 2021 in Technology by JackTerrance
0 votes
    In java, can we use the string buffer functions for string chapter ? Select the correct answer from above options...
asked Dec 24, 2021 in Education by JackTerrance
0 votes
    How can you start a JTA transaction from a Servlet deployed on JBoss?...
asked Jun 5, 2021 in Technology by JackTerrance
0 votes
    Which of the following attribute triggers event when media can start play, but might has to stop for buffering? A - onbeforeplay B - onplay C - oncanplay D - oncanplaythrough...
asked Dec 3, 2020 in Technology by JackTerrance
...