The document explains the thread life cycle in Java, detailing states such as New, Runnable, Blocked, Waiting, Timed Waiting, and Terminated. It also describes two primary methods for creating threads: extending the Thread class and implementing the Runnable interface, highlighting the advantages of each approach. Implementing Runnable is generally preferred for its flexibility in real-world applications.
The document explains the thread life cycle in Java, detailing states such as New, Runnable, Blocked, Waiting, Timed Waiting, and Terminated. It also describes two primary methods for creating threads: extending the Thread class and implementing the Runnable interface, highlighting the advantages of each approach. Implementing Runnable is generally preferred for its flexibility in real-world applications.
There are multiple states of a thread in its lifecycle, as explained below:
1️) New Thread (NEW State) When a new thread is created, it is in the new state. The thread has not started execution yet and remains in this state until the start () method is called. 2️) Runnable State A thread moves to the Runnable state once the start () method is called. In this state, the thread is ready to run, but it depends on the CPU scheduler to decide when it will execute. In a multi-threaded program, multiple threads share CPU time, and the thread scheduler allocates time to each thread. 3️) Blocked State A thread enters the Blocked state when it tries to acquire a lock (e.g., entering a synchronized block) but another thread is already holding the lock. The thread remains in this state until it gets the required lock, after which it moves back to Runnable. 4️) Waiting State A thread enters the Waiting state when it calls wait () or join (). It stays here until another thread notifies it or the thread it was waiting for terminates. Examples: • wait (): A thread waits for another thread to complete a task. • join (): A thread waits for another thread to finish before continuing execution. 5️) Timed Waiting State A thread moves into Timed Waiting state when it waits for a specific time. This happens when methods like sleep (), join(time), or wait(time) are used. The thread will resume after the time expires or if it gets a notification. Example: Thread.sleep(5000); // Moves to TIMED_WAITING for 5 seconds 6️) Terminated State A thread enters the Terminated state when it has completed execution or is stopped due to an error. A thread terminates in two cases: 1. Normal completion: The thread completes execution successfully. 2. Unexpected termination: The thread is stopped due to an exception or error. Ways to Create a Thread in Java In Java, there are two main ways to create a thread: 1️) By Extending the Thread Class Java provides a built-in Thread class that can be extended to create a new thread. To do this, we need to override the run() method, which defines the code that will be executed by the thread. After creating the thread, we use the start() method to begin execution. Example: class MyThread extends Thread { public void run() { System.out.println("Thread is running..."); } }
public class Main {
public static void main(String[] args) { MyThread t = new MyThread(); // Creating a thread t.start(); // Starting the thread } } 2️) By Implementing the Runnable Interface Instead of extending Thread, we can implement the Runnable interface. This method is more flexible because the class can still extend another class while implementing Runnable. Here, the run() method is defined inside a separate class, and we pass an instance of this class to a Thread object. Example: class MyRunnable implements Runnable { public void run() { System.out.println("Thread is running..."); } } public class Main { public static void main(String[] args) { Thread t = new Thread(new MyRunnable()); // Creating a thread t.start(); // Starting the thread } }
Which Method to Choose?
• Use Thread class when you don’t need multiple inheritance and want a quick way to create a thread. • Use Runnable interface when you need more flexibility, especially when your class needs to extend another class. Both approaches are commonly used, but implementing Runnable is generally preferred in real-world applications.
Download ebooks file (Ebook) Operations Research and Cyber-Infrastructure by William E. Hart (auth.), John W. Chinneck, Bjarni Kristjansson, Matthew J. Saltzman (eds.) ISBN 9780387888422, 9780387888439, 038788842X, 0387888438 all chapters