Java - Thread Life Cycle

Hello there, aspiring Java programmers! Today, we're going to dive into one of the most exciting aspects of Java programming: the life cycle of threads. Don't worry if you're new to programming; I'll guide you through this journey step by step, just as I've done for countless students over my years of teaching. So, grab a cup of coffee (or tea, if that's your thing), and let's embark on this thrilling adventure together!

Java - Thread Life Cycle

What is a Thread?

Before we jump into the life cycle, let's start with the basics. Imagine you're in a busy kitchen. The chef is chopping vegetables, the sous chef is stirring a pot, and the pastry chef is decorating a cake. Each of these tasks is happening simultaneously, right? Well, in the world of programming, these tasks would be like threads!

A thread is the smallest unit of execution in a program. It's like a mini-program running within your main program, allowing different parts of your code to run concurrently.

The Life Cycle of a Thread

Now, let's talk about the life cycle of a thread. Just like how we humans go through different stages in life (baby, toddler, teenager, adult), threads also go through various states during their lifetime.

States of a Thread Life Cycle in Java

  1. New
  2. Runnable
  3. Running
  4. Blocked/Waiting
  5. Terminated

Let's break these down with some examples, shall we?

1. New State

When you create a thread, it's in the "New" state. It's like a baby that has just been born but hasn't started crawling yet.

Thread myThread = new Thread();

In this example, we've created a new thread called myThread, but it hasn't started running yet.

2. Runnable State

Once you call the start() method on your thread, it enters the "Runnable" state. It's ready to run, but it's waiting for the thread scheduler to pick it up and execute it.

myThread.start();

Now myThread is like a toddler, eager to explore but waiting for mom or dad to take them to the park.

3. Running State

When the thread scheduler selects the thread from the runnable pool, it enters the "Running" state. This is where the thread is actually executing its task.

public void run() {
System.out.println("Thread is running!");
}

This is the content of your run() method, which defines what the thread will do when it's in the running state.

4. Blocked/Waiting State

Sometimes, a thread might need to wait for a resource or for another thread to complete its task. In this case, it enters the "Blocked" or "Waiting" state.

synchronized(object) {
try {
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

In this example, the thread is waiting for object to notify it before it can continue.

5. Terminated State

Finally, when a thread completes its task or if an uncaught exception occurs, it enters the "Terminated" state. It's like retirement for threads!

public void run() {
System.out.println("Thread is running!");
// Thread task completed
}

Once this run() method completes, the thread will terminate.

Flow Chart of Java Thread Life Cycle

To visualize this better, let's look at a flow chart:

[New] --> [Runnable] --> [Running] --> [Terminated]
^    |           |
|    |           |
|    v           v
|  [Blocked/Waiting]
|___________________|

Java Example for Demonstrating Thread States

Now, let's put it all together with a complete example:

public class ThreadLifeCycleDemo implements Runnable {
public static Thread thread1;
public static ThreadLifeCycleDemo obj;

public static void main(String[] args) {
obj = new ThreadLifeCycleDemo();
thread1 = new Thread(obj);

// Thread is in NEW state
System.out.println("State of thread1 after creating it - " + thread1.getState());
thread1.start();

// Thread is in RUNNABLE state
System.out.println("State of thread1 after calling .start() method on it - " + thread1.getState());
}

public void run() {
thread myThread = new thread(new ThreadLifeCycleDemo());
// Thread is in NEW state
System.out.println("State of myThread after creating it - " + myThread.getState());
myThread.start();

// Thread is in RUNNABLE state
System.out.println("State of myThread after calling .start() method on it - " + myThread.getState());

try {
// moving thread1 to timed waiting state
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("State of myThread after calling .sleep() method on it - " + myThread.getState());

try {
// waiting for myThread to die
myThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("State of myThread when it has finished execution - " + myThread.getState());
}
}

This example demonstrates all the states a thread goes through. When you run this program, you'll see output showing the different states of the threads as they progress through their life cycles.

Conclusion

And there you have it, folks! We've journeyed through the life cycle of a Java thread, from its birth in the "New" state to its final rest in the "Terminated" state. Remember, understanding thread states is crucial for writing efficient multithreaded applications.

Just like in life, threads go through various stages, each with its own characteristics and challenges. By mastering these concepts, you're one step closer to becoming a Java threading expert!

Keep practicing, keep coding, and most importantly, keep having fun with Java! Who knows, maybe one day you'll be teaching this to a new generation of programmers. Until next time, happy coding!

Terjemahan ke Bahasa Melayu (ms):

Java - Kitaran Hidup Thread

Hai sana, para pemrogram Java yang sedang belajar! Hari ini, kita akan melihat salah satu aspek yang paling menarik dalam pemrograman Java: kitaran hidup thread. Jangan khawatir jika anda baru dalam bidang pemrograman; saya akan menghidangkan anda secara berperingkat, sama seperti yang telah saya lakukan untuk ribuan pelajar sepanjang tahunajaran saya. Jadi, ambil secangkir kopi (atau teh, jika itu hal yang anda sukai), dan mari kita melangkah ke dalam pengembaraan yang menarik ini bersama!

Apa Itu Thread?

Sebelum kita melompat ke kitaran hidup, mari kita mulai dengan dasar-dasar. Bayangkan anda di dapur yang sibuk. Chef sedang memotong sayur, chef pembantu sedang mengaduk kuali, dan chef kue sedang menghias kue. Setiap tugas ini sedang berlangsung secara bersamaan, kan? Di dunia pemrograman, tugas-tugas ini akan seperti thread!

Thread adalah unit eksekusi terkecil dalam program. Itu seperti program mini yang berjalan dalam program utama anda, membolehkan bahagian-bahagian berbeza dalam kod anda untuk berjalan secara bersamaan.

Kitaran Hidup Thread

Sekarang, mari kita bicarakan tentang kitaran hidup thread. Seperti bagaimana manusia kita melalui berbagai peringkat dalam kehidupan (bayi, kanak-kanak, remaja, dewasa), thread juga melalui beberapa keadaan selama hayatannya.

Keadaan Kitaran Hidup Thread dalam Java

  1. New
  2. Runnable
  3. Running
  4. Blocked/Waiting
  5. Terminated

mari kita pecahkan ini dengan beberapa contoh, kenapa tidak?

1. Keadaan New

Bila anda mencipta thread, ia berada dalam keadaan "New". Itu seperti bayi yang baru lahir tapi belum mulai merangkak.

Thread myThread = new Thread();

Dalam contoh ini, kita telah mencipta thread baru bernama myThread, tapi ia belum dimulakan.

2. Keadaan Runnable

Sekali anda memanggil method start() pada thread anda, ia memasuki keadaan "Runnable". Ia siap untuk berjalan, tapi menunggu scheduler thread untuk memilih dan menjalankannya.

myThread.start();

Sekarang myThread seperti kanak-kanak yang sabar untuk mengenal dunia tetapi menunggu ibu atau bapa untuk membawanya ke taman.

3. Keadaan Running

Bila scheduler thread memilih thread dari pool yang boleh dijalankan, ia memasuki keadaan "Running". Ini adalah tempat thread sebenarnya menjalankan tugasnya.

public void run() {
System.out.println("Thread is running!");
}

Ini adalah isi method run() anda, yang menentukan apa yang akan dilakukan thread ketika ia dalam keadaan running.

4. Keadaan Blocked/Waiting

Kadang-kadang, thread mungkin perlu menunggu sumber atau penyelesaian tugas thread lain. Dalam kes ini, ia memasuki keadaan "Blocked" atau "Waiting".

synchronized(object) {
try {
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

Dalam contoh ini, thread menunggu object untuk memberitahu sebelum ia dapat meneruskan.

5. Keadaan Terminated

Akhirnya, bila thread menyelesaikan tugasnya atau jika terjadi pengecualian yang belum ditangkap, ia memasuki keadaan "Terminated". Itu seperti pensiun bagi thread!

public void run() {
System.out.println("Thread is running!");
// Tugas thread selesai
}

Setelah method run() ini selesai, thread akan berakhir.

Carta Kitaran Hidup Thread Java

Untuk memvisualisasikan ini lebih baik, mari kita lihat carta:

[New] --> [Runnable] --> [Running] --> [Terminated]
^    |           |
|    |           |
|    v           v
|  [Blocked/Waiting]
|___________________|

Contoh Java untuk Menunjukkan Keadaan Thread

Sekarang, mari kita gabungkan semua ini dengan contoh lengkap:

public class ThreadLifeCycleDemo implements Runnable {
public static Thread thread1;
public static ThreadLifeCycleDemo obj;

public static void main(String[] args) {
obj = new ThreadLifeCycleDemo();
thread1 = new Thread(obj);

// Thread berada dalam keadaan NEW
System.out.println("State of thread1 after creating it - " + thread1.getState());
thread1.start();

// Thread berada dalam keadaan RUNNABLE
System.out.println("State of thread1 after calling .start() method on it - " + thread1.getState());
}

public void run() {
thread myThread = new thread(new ThreadLifeCycleDemo());
// Thread berada dalam keadaan NEW
System.out.println("State of myThread after creating it - " + myThread.getState());
myThread.start();

// Thread berada dalam keadaan RUNNABLE
System.out.println("State of myThread after calling .start() method on it - " + myThread.getState());

try {
// memindahkan thread1 ke keadaan menunggu berjenjang
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("State of myThread after calling .sleep() method on it - " + myThread.getState());

try {
// menunggu myThread mati
myThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("State of myThread when it has finished execution - " + myThread.getState());
}
}

Contoh ini menunjukkan semua keadaan yang dijalani thread. Ketika anda menjalankan program ini, anda akan melihat output yang menunjukkan keadaan berbeza thread sebagaimana mereka maju melalui kitaran hidup mereka.

Kesimpulan

Dan begitu juga, teman-teman! Kita telah menjalani kitaran hidup thread Java, dari kelahirannya dalam keadaan "New" hinggalah ke peristirahatannya dalam keadaan "Terminated". Ingat, memahami keadaan thread adalah penting bagi menulis aplikasi multithreaded yang efisien.

Seperti dalam kehidupan, thread melalui berbagai peringkat, masing-masing dengan ciri-ciri dan cabaran tersendiri. Dengan memahami konsep-konsep ini, anda sudah satu langkah lebih dekat untuk menjadi ahli pemrograman threading Java!

Terus latih, terus kod, dan terutama, terus bersenang-senang dengan Java! Siapa tahu, mungkin suatu hari anda akan mengajarkan ini kepada generasi pemrogram baru. Sampaijumpa lagi, kod yang gembira!

Credits: Image by storyset