BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Can we start a Thread twice in Java?

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

Can we start a thread twice in Java? The answer is no, once a thread is started, it can never be started again. Doing so will throw an IllegalThreadStateException. Lets have a look at the below code:

public class ThreadTwiceExample implements Runnable {
   @Override
   public void run(){  
	Thread t = Thread.currentThread();
        System.out.println(t.getName()+" is executing.");
		    
   }  
   public static void main(String args[]){  
	Thread th1 = new Thread(new ThreadTwiceExample(), "th1"); 
	th1.start();  
	th1.start();  
   } 
}

Output:

Exception in thread "main" th1 is executing.
java.lang.IllegalThreadStateException

As you observe the first call to start() resulted in execution of run() method, however the exception got thrown when we tried to call the start() second time.

Top Related Articles:

  1. OOPs in Java: Encapsulation, Inheritance, Polymorphism, Abstraction
  2. What is the difference between a process and a thread in Java?
  3. Constructor Overloading in Java with examples
  4. Cloneable Interface in Java – Object Cloning
  5. Basics: All about Java threads

Tags: Java-Multithreading

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. juvin says

    July 17, 2015 at 2:23 AM

    hello,
    you are doing a very good job in clarifying the java basics for the newbies …
    please keep doing it more and more on more topics.. :) (greedy me :p )

    i have a doubt.
    the same thread cannot be started multiple times. what if i instantiate the class again and then start it again?
    ti = new ThreadclassName();
    ti.start;
    t2= new ThreadclassName();
    t2.start();

    is this possible?

    Reply
    • Neeraj says

      July 27, 2015 at 2:24 PM

      Hi Juvin,
      It is possible if we instantiate same class again and invoke run method by start, the program will compile successful.Now we are not running same thread.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap