From 35f55ed8e4b722b3bf7787a5e38ce869b6acb315 Mon Sep 17 00:00:00 2001
From: "Na, Zhenye" <32248549+Zhenye-Na@users.noreply.github.com>
Date: Wed, 24 May 2023 22:55:01 -0700
Subject: [PATCH] Create blog post of JUC
Java Concurrent Programming 1 - Fundamentals
---
...mentals-of-java-concurrenct-programming.md | 367 ++++++++++++++++++
1 file changed, 367 insertions(+)
create mode 100644 _posts/2023-05-24-fundamentals-of-java-concurrenct-programming.md
diff --git a/_posts/2023-05-24-fundamentals-of-java-concurrenct-programming.md b/_posts/2023-05-24-fundamentals-of-java-concurrenct-programming.md
new file mode 100644
index 0000000..cd41d10
--- /dev/null
+++ b/_posts/2023-05-24-fundamentals-of-java-concurrenct-programming.md
@@ -0,0 +1,367 @@
+---
+layout: article
+title: "Java Concurrent Programming 1 - Fundamentals"
+date: 2023-05-24
+modify_date: 2023-05-24
+excerpt: "Pre-requisite knowlesge you need all to learn Java Concurrent Programming"
+tags: [Java, Concurrent Programming]
+mathjax: false
+mathjax_autoNumber: false
+key: fundamentals-of-java-concurrenct-programming
+---
+
+Concurrency plays a crucial role in developing efficient and responsive Java applications. Understanding the fundamentals of Java concurrent programming is essential for writing robust and scalable code. In this blog post, we'll explore several key aspects of Java concurrent programming, including the lifecycle of a Java thread, different methods to create a Java thread, inter-thread communication, mechanisms for thread safety.
+
+## 1. Lifecycle of Java Thread
+
+A Java thread has a well-defined lifecycle, consisting of the following states:
+
+- `NEW`:** The thread is in this state before it is started. Note: `start()` method has not been invoked yet.
+- `RUNNABLE`:** The thread is ready to run, and it can be scheduled by the Java Virtual Machine (JVM).
+- `RUNNING`:** The thread is currently executing its code.
+- `BLOCKED`:** The thread is temporarily inactive and cannot proceed until a certain condition is satisfied.
+- `WAITING`:** The thread is waiting for other threads action, without time limit
+- `TIME_WATITING` The thread is waiting for a specific period of time.
+- `TERMINATED`:** The thread has completed its execution or has been stopped.
+
+
+
+
+
+***
+
+## 2. Different Methods to Create Java Thread
+
+Java provides several methods to create and start threads, each with its own advantages and use cases. Let's explore four commonly used methods:
+
+### 2.1 Extending the `Thread` class
+
+In this approach, you can create a new class by extending the `Thread` class and overriding its `run()` method. The `run()` method contains the code that will be executed by the thread when started. Here's an example:
+
+```java
+public class MyThread extends Thread {
+ @Override
+ public void run() {
+ // Thread's behavior goes here
+ }
+}
+
+// Creating and starting the thread
+MyThread thread = new MyThread();
+thread.start();
+
+```
+
+**Pros:**
+
+This method allows you to directly work with the `Thread` class and provides flexibility in defining the behavior of the thread.
+
+
+**Cons:**
+
+In Java, a class can only inherit one parent class. In this case, any resource class that could potentially be improved by Concurrency need to extends Thread class, which add the limitation to extends other business related classes.
+
+
+### 2.2 Implementing the `Runnable` interface
+
+Another way to create a thread is by implementing the `Runnable` interface. This approach separates the thread's behavior from the `Thread` class, promoting better separation of concerns. Here's an example:
+
+```java
+public class MyRunnable implements Runnable {
+ @Override
+ public void run() {
+ // Thread's behavior goes here
+ }
+}
+
+// Creating and starting the thread
+Thread thread = new Thread(new MyRunnable());
+thread.start();
+
+```
+
+By implementing `Runnable` interface, you can reuse the same instance of the behavior in multiple threads, enhancing code modularity.
+
+### 2.3 Using the `ExecutorService` framework (`ThreadPool`)
+
+The `ExecutorService` framework provides a higher-level abstraction for managing and executing threads. It handles the creation, pooling, and lifecycle management of threads, allowing you to focus on the tasks to be executed. Here's an example:
+
+```java
+ExecutorService executor = Executors.newFixedThreadPool(5);
+
+// 1) Executing a task
+// execute() method take in a instance that implement Runnable interface
+executor.execute(new ERunnable() {
+ @Override
+ public void run() {
+ // Thread's behavior goes here
+ }
+})
+
+// 2) Submitting a task to the executor for execution
+Future