• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Multithreading » Thread Life Cycle in Java
Next →
← Prev

Thread Life Cycle in Java

By Dinesh Thakur

A thread can undergo some states during its life cycle. It is because in a multithreaded environment when multiple threads are executing only one thread can use the CPU at a time, and all other threads should be in some other states either waiting for their turn for the CPU or waiting for some other condition to be satisfied.

A thread is always in one of five states: newborn, runnable, running, dead and blocked. Figure shows the life cycle of a thread.

We’ll be covering the following topics in this tutorial:

  • The newborn state
  • The runnable state
  •  The running state
  • The dead state
  • The blocked state
  •  Manipulating threads

The newborn state

When a thread is called, it is in the newborn state, that is, when it has been created and is not yet running. In other words, a start () method has not been invoked on this thread. In this state, system resources are not yet allocated to the thread. When a thread is in the newborn state, calling any method other than start () method causes an IllegalThreadStateException.

The runnable state

A thread in the runnable state is ready for execution but is not being executed currently. Once a thread is in the runnable state, it gets all the resources of the system (as, for example, access to the CPU) and moves on to the running state.A thread may be in one of the following states in its life cycleAll runnable threads are in a queue and wait for CPU access. When the start () method is called on the newborn thread, it will be in the runnable state. It is not in the running state yet because system resources such as the CPU (which might be serving another thread at that point) are not available.

 The running state

After the runnable state, if the thread gets CPU access, it moves into the running state. The thread will be in the running state unless one of the following things occur:

• It dies (that is, the run () method exits).

• It gets blocked to the input/output for some reason.

• It calls sleep ().

• It calls wait ().

• It calls yield ().  

• It is preempted by a thread of higher priority.

• Its quantum (time slice) expires.

A thread with a higher priority than the running thread can preempt the running thread if any of the following situations arise:

• If the thread with the higher priority comes out of the sleeping state.

• The I/O completes for a thread with a higher priority waiting for that I/O.

• Either notify () or notifyAll () is called on a thread that called wait.

When a thread calls the wait () method it goes in the waiting state. To wake up this thread, some other running thread should notify it.

The dead state

A thread goes into the dead state in two ways:

• If its run () method exits.

• A stop () method is invoked.

The run () method exits when it finished execution naturally or throws an uncaught exception. The stop () method kills the thread. A thread in the dead state cannot be executed further.

The blocked state

A thread can enter the blocked state when one of the following five conditions occurs:

• When sleep () is called,

• When suspend () is called,

• When wait () is called,

• The thread calls an operation, (For example, during input/output, a thread will not return until the I/O operation completes.),

• The thread is waiting for monitor.

A thread in the blocked state waits for some action to happen so that it can get ready. That is, if the thread requires some of the I/O operation, it will enter into the blocked state by giving the access to the CPU to another thread. After completion of the I/O operations it will enter into the runnable state.

A thread must move out of a blocked state into the runnable (or running) state using the opposite of whatever phenomenon put it into the blocked state.

• If a thread has been put to sleep (), the specified timeout period must expire.

• If a thread has called wait (), then some other thread using the resource for which the first thread is waiting must call notify () or notifyAll ().

• If a thread is waiting for the completion of an input or output operation, then the operation must finish.

 Manipulating threads

Table lists some of the methods used to manipulate threads.

In addition to the methods described in Table, there are some which need to be discussed briefly. They include the sleep (), suspend (), resume (), wait (), notify (), notifyAll () and yield () methods.

Table. Some methods that are provided in the Thread class to manipulate threads

Name of the method

Function

getName()                                   

returns name of the thread

setPriority ()                                 

to set the priority of the thread

getPriority ()                                

returns the thread priority

isAlive ()                                    

Determines whether the thread is still running or not; isAlive () returns true if the method is running, Runnable or blocked. It returns false if the method is a new thread or dead

join ()                                         

wait for a thread to terminate

run ()                                          

entry point for the thread

start()                                         

start a thread by calling its run method

stop()                                         

terminate the thread abruptly

sleep ()

A thread being executed can invoke sleep () to block the thread for some time and free the CPU. This thread goes into the sleep (blocked) state for the specified amount of time, after which it moves to the runnable state. The sleep method has the following prototypes:

public static void sleep (long millisec) throws InterruptedException;

public static void sleep (long millisec, int nanosec) throws InterruptedException;

suspend () and resume ()

The Thread class has a method suspend () to stop the thread temporarily and a method resume () to restart it at the point at which it is halted. The resume () method must be called by some thread other than the suspended one. The suspend () method is not recommended in application programming.

Wait (), notify () and notifyAll ()

When a running thread calls wait (), it enters into a waiting (blocked) state for the particular object on which wait was called. A thread in the waiting state for an object becomes ready on a call to notify () issued by another thread associated with that object. All threads waiting for a given object become ready when receiving a call to notifyAll () from another thread associated with that object.

yield ()

Some CPU-intensive operations of one thread may prevent other threads from being executed. To prevent this from happening, the first one can allow other threads to execute by invoking the yield () method. This method allows another thread of the same priority to be run. The thread from which yield () is invoked moves from the running state to the runnable state.

The methods sleep () and yield () are static methods.

You’ll also like:

  1. Applet Life Cycle in Java with Example
  2. Java Servlet Life Cycle
  3. Life Cycle of JSP
  4. What is Systems Development Life Cycle?
  5. Software Maintenance Life Cycle
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW