As a programming language that is particularly suitable for building multi-threaded applications, Java can make full use of the advantages of multi-core processors to improve program concurrency and efficiency. However, during multi-threaded development, the issue of communication between threads becomes a key challenge. This article will introduce several common methods for dealing with inter-thread communication problems.
Shared variables are one of the simplest and most common methods of inter-thread communication. Multiple threads can pass information by accessing and modifying shared variables. However, since threads execute in parallel, race conditions may occur. To avoid race conditions, we need to use a mutex to protect access to shared variables. Mutex locks can be implemented in Java using the synchronized keyword or Lock interface.
The following is a sample code that uses shared variables for thread communication:
public class SharedVariableExample { private int sharedVar = 0; public synchronized void increment() { sharedVar++; } public synchronized int getSharedVar() { return sharedVar; } } public class MyThread extends Thread { private SharedVariableExample example; public MyThread(SharedVariableExample example) { this.example = example; } public void run() { for (int i = 0; i < 10; i++) { example.increment(); } } } public class Main { public static void main(String[] args) { SharedVariableExample example = new SharedVariableExample(); MyThread thread1 = new MyThread(example); MyThread thread2 = new MyThread(example); thread1.start(); thread2.start(); try { thread1.join(); thread2.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("SharedVar: " + example.getSharedVar()); } }
In the above example, the two threads perform 10 increment operations on the shared variables respectively, through join() The method waits for all threads to finish executing and prints the value of the shared variable.
When using shared variables for inter-thread communication, if a thread needs to wait for the results of another thread, we can use the wait/notification mechanism (Wait/Notify Mechanism). When a thread needs to wait, it can call the object's wait() method to put the thread into a waiting state. When a certain condition is met, other threads call the object's notify() method to wake up the waiting thread.
The following is a sample code that uses the wait/notification mechanism for thread communication:
public class WaitNotifyExample { private boolean flag = false; public synchronized void waitForSignal() { while (!flag) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } flag = false; System.out.println("Received signal"); } public synchronized void sendSignal() { flag = true; notify(); } } public class WaitThread extends Thread { private WaitNotifyExample example; public WaitThread(WaitNotifyExample example) { this.example = example; } public void run() { example.waitForSignal(); } } public class NotifyThread extends Thread { private WaitNotifyExample example; public NotifyThread(WaitNotifyExample example) { this.example = example; } public void run() { example.sendSignal(); } } public class Main { public static void main(String[] args) { WaitNotifyExample example = new WaitNotifyExample(); WaitThread waitThread = new WaitThread(example); NotifyThread notifyThread = new NotifyThread(example); waitThread.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } notifyThread.start(); try { waitThread.join(); notifyThread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } }
In the above example, the WaitThread thread waits for the signal to be received, and the NotifyThread thread sends the signal through sleep() The method waits for a period of time and then wakes up the waiting thread.
Blocking Queue is an efficient way to achieve communication between threads. It provides put() and take() methods, which can automatically block and wait when the queue is full or empty until the conditions are met.
The following is a sample code that uses blocking queues for thread communication:
import java.util.concurrent.ArrayBlockingQueue;
The above is the detailed content of How to deal with inter-thread communication issues in Java development. For more information, please follow other related articles on the PHP Chinese website!