Tools: Java Multithreading/Concurrency

Tools: Java Multithreading/Concurrency

Source: Dev.to

Multithreading in Java is a feature that allows for the concurrent execution of two or more parts of a program, known as threads, to maximize the utilization of the CPU. Each thread can run in parallel with others, sharing the same memory space.
Key Concepts
Thread: A thread is a lightweight sub-process, the smallest unit of processing. Multiple threads can exist within a single process.
Concurrency vs. Parallelism:
Concurrency: Multiple threads make progress in an interleaved manner on a single CPU core. The system switches between threads, giving the illusion of simultaneous execution.
Parallelism: Multiple threads run simultaneously on different CPU cores. This is true parallel execution.
Main Purpose:
Performance: To perform CPU-intensive tasks in parallel, speeding up execution time on multi-core processors.
Responsiveness: To keep an application responsive. For example, a long-running task (like a network request or file download) can run in a background thread without freezing the user interface. In Java, a thread can be in one of the following states, which are defined in the java.lang.Thread.State enum:
NEW
A thread that has been created but has not yet started. It remains in this state until the start() method is invoked on it.
RUNNABLE
A thread that is executing in the Java Virtual Machine (JVM). A thread in this state is either currently running or is ready to run and waiting for its turn to be selected by the thread scheduler.
BLOCKED
A thread that is waiting to acquire a monitor lock to enter a synchronized block or method. It is blocked because another thread currently holds the lock.
WAITING
A thread that is waiting indefinitely for another thread to perform a particular action. A thread enters this state by calling one of the following methods: TIMED_WAITING
A thread that is waiting for a specified period of time. A thread enters this state by calling one of these methods with a timeout value: TERMINATED
A thread that has completed its execution. This happens when its run() method has finished, either by completing normally or by throwing an unhandled exception Synchronization in Java is a mechanism to control access to shared resources by multiple threads to prevent race conditions and ensure thread safety.
Key Concepts Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse CODE_BLOCK:
class MyRunnable implements Runnable { public void run() { System.out.println("Thread is running by implementing Runnable."); }
} // To start the thread:
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start(); Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
class MyRunnable implements Runnable { public void run() { System.out.println("Thread is running by implementing Runnable."); }
} // To start the thread:
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start(); CODE_BLOCK:
class MyRunnable implements Runnable { public void run() { System.out.println("Thread is running by implementing Runnable."); }
} // To start the thread:
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start(); CODE_BLOCK:
class MyThread extends Thread { public void run() { System.out.println("Thread is running by extending Thread."); }
} // To start the thread:
MyThread thread = new MyThread();
thread.start(); Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
class MyThread extends Thread { public void run() { System.out.println("Thread is running by extending Thread."); }
} // To start the thread:
MyThread thread = new MyThread();
thread.start(); CODE_BLOCK:
class MyThread extends Thread { public void run() { System.out.println("Thread is running by extending Thread."); }
} // To start the thread:
MyThread thread = new MyThread();
thread.start(); CODE_BLOCK:
Object.wait() (with no timeout)
Thread.join() (with no timeout)
LockSupport.park() Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
Object.wait() (with no timeout)
Thread.join() (with no timeout)
LockSupport.park() CODE_BLOCK:
Object.wait() (with no timeout)
Thread.join() (with no timeout)
LockSupport.park() CODE_BLOCK:
Thread.sleep(long millis)
Object.wait(long timeout)
Thread.join(long millis)
LockSupport.parkNanos(long nanos)
LockSupport.parkUntil(long deadline) Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
Thread.sleep(long millis)
Object.wait(long timeout)
Thread.join(long millis)
LockSupport.parkNanos(long nanos)
LockSupport.parkUntil(long deadline) CODE_BLOCK:
Thread.sleep(long millis)
Object.wait(long timeout)
Thread.join(long millis)
LockSupport.parkNanos(long nanos)
LockSupport.parkUntil(long deadline) CODE_BLOCK:
public synchronized void updateData() { // Only one thread can execute this at a time
} public void updateData() { synchronized(this) { // Only this section is synchronized }
} Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
public synchronized void updateData() { // Only one thread can execute this at a time
} public void updateData() { synchronized(this) { // Only this section is synchronized }
} CODE_BLOCK:
public synchronized void updateData() { // Only one thread can execute this at a time
} public void updateData() { synchronized(this) { // Only this section is synchronized }
} - What is multithreading in Java? - How to Create Threads in Java
There are two primary ways to create a thread:
Implementing the Runnable Interface (Preferred Method):
Create a class that implements the java.lang.Runnable interface.
Implement the run() method, which contains the code to be executed by the thread.
Create an instance of the Thread class, passing your Runnable object to its constructor, and call the start() method. - What are the different state of thread. - Explain Synchronization - The Problem It Solves
Race Condition: When multiple threads access and modify shared data simultaneously, leading to unpredictable results
Memory Consistency Errors: When different threads have inconsistent views of the same data
- How It Works
Every object in Java has an intrinsic lock (monitor). When a thread acquires this lock, other threads must wait until it's released.
- Two Main Approaches