🐑 Java Thread Life Cycle

GunhoΒ·2025λ…„ 1μ›” 22일

🐑 Java Thread

🐑 A thread is a workflow of a process where in Java, threads share static and heap memory within the JVM.

In Java, a thread can be executed via running the main method which will invoke the main thread to run. Otherwise, to run extra threads other than the main thread, two further approaches can be introduced:

  • πŸͺ‘ Thread Class
  • πŸƒ Runnable Interface

πŸͺ‘ Thread Class

One way is to extend a Thread Class and implement a run() method. The extending class then can be instantiated and by invoking the start() method, the designed actions can run in parallel:

Thread Class

class MyThread extends Thread {
	@Override
    public void run() {
    	System.out.println("NEW THREAD");
    }
}

public class Main {
	public static void main(String[] args) {
    	Thread thread = new MyThread();
        thread.start();
    }
}

πŸƒ Runnable Interface

Another way is to implement the Runnable Interface and to implement the run() method like the above approach. Executing the thread however slightly varies as it requires both Runnable class and a Thread class running the Runnable class to be instantiated:

Runnable Interface

class MyRunnable implements Runnable {
	@Override
    public void run() {
        System.out.println("NEW THREAD");
    }
}

public class Main {
    public static void main(String[] args) {
        Thread thread = new Thread(new MyRunnable());
        thread.start();
    }
}

πŸ’‘ A thread can still run with the run() method but the run() method will use the existing resources while start() will assign a new resource hence creating a new thread

It is often said that running extra threads via Runnable Interface may be more flexible and reusable followed by Java's nature that Java does not allow multiple inheritances.


🦭 Java Thread Life Cycle

The Life Cycle of Thread can be best described as the below:

  • ✨ New

  • πŸ‘·β€β™‚οΈ Runnable

  • ⭕️ Blocked

  • πŸ§β€β™‚οΈ Waiting

  • ⏱️ Timed Waiting

  • πŸ—‘οΈ Terminated

Baeldung Available here


✨ New

A state where a thread is just created before the start() method has been invoked.

eg

Thread thread = new Thread(runnable);

πŸ‘·β€β™‚οΈ Runnable

A state where a thread is is ready to run. The start() method has been invoked.

Java cannot exactly compare between a thread actually running or waiting in the scheduler's queue

eg

thread.start();

⭕️ Blocked

A state where a thread is waiting to acquire a monitor lock.

Blocked State implies there already is another thread running inside the synchronized block after acquiring the monitor lock.

eg

thread.... 

synchronized(lock) {
	another thread....
}

πŸ§β€β™‚οΈ Waiting

A state where a thread indefinitely waits for other threads to complete their tasks.

Waiting can be invoked from wait(), and join() methods and can make a thread to run again by invoking notify() or notifyAll() methods.

eg

object.wait();

⏱️ Timed Waiting

A state that is invoked from the Thread.sleep() method. wait() and join() methods with the time parameters can also make threads fall in this state.

eg

Thread.sleep(1000);

πŸ—‘οΈ Terminated

A state where the designed execution of a thread is complete.


🎣 Thread Optimisation

Thread optimisation is an important process for improving performance in a multithreaded environment. To optimise threads, it is recommended to use a thread pool.

A thread pool pre-creates a fixed number of threads and reuses them whenever tasks becomes terminated. Hence, using a thread pool could reduce the cost or overhead associated with creating and destroying threads.

One common way to create a thread pool is via Executors as below:

eg

public class Main {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(1000);
        
        for (int i = 0; i < 1000; i++) {
            executor.submit(new MyRunnable());
        }
        
        executor.shutdown();
    }
}

πŸ“š References

μš°μ•„ν•œ ν…Œν¬ μ½”μŠ€
Baeldung

profile
Hello

0개의 λŒ“κΈ€