文章目录
- 1. wait() 和 notify()
- 2. volatile 关键字
- 3. Java.util.concurrent 包提供的工具类
- Semaphore(信号量)
- BlockingQueue(阻塞队列)
- 4. Atomic 类
在 Java 中,线程间的通信是非常重要的,尤其是在多线程编程中,它有助于协调线程的行为,确保资源的正确访问和更新。Java 提供了多种方式来实现线程间的通信,主要包括以下几种方法:
1. wait() 和 notify()
这是最基础的线程间通信方式之一,基于 Object
类中的 wait()
和 notify()
方法。它们通常用于实现生产者-消费者模式。
实现示例:
public class WaitNotifyExample {private final Object lock = new Object();private boolean flag = false;public void producer() {synchronized(lock) {if (flag) {try {lock.wait();} catch (InterruptedException e) {e.printStackTrace();}}// 生产一些数据...flag = true;lock.notify(); // 通知消费者}}public void consumer() {synchronized(lock) {if (!flag) {try {lock.wait();} catch (InterruptedException e) {e.printStackTrace();}}// 消费数据...flag = false;lock.notify(); // 通知生产者}}
}
2. volatile 关键字
volatile
关键字可以确保变量的可见性,即一个线程修改了一个共享变量的值,另一个线程能立即看到这个变化。
实现示例:
public class VolatileExample {private volatile boolean running = true;public void startThread() {Thread thread = new Thread(() -> {while (running) {// 执行一些任务}System.out.println("线程停止");});thread.start();try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}running = false; // 通知线程停止}
}
3. Java.util.concurrent 包提供的工具类
Java 提供了 java.util.concurrent
包来简化线程间的通信,其中包括 CountDownLatch
, CyclicBarrier
, Semaphore
, Exchanger
, Future
和 BlockingQueue
等。
实现示例:
Semaphore(信号量)
信号量用于控制同时访问特定资源的线程数量。
import java.util.concurrent.Semaphore;public class SemaphoreExample {private final Semaphore semaphore = new Semaphore(1);public void acquireAccess() throws InterruptedException {semaphore.acquire(); // 获取许可try {// 访问资源} finally {semaphore.release(); // 释放许可}}
}
BlockingQueue(阻塞队列)
阻塞队列是用于存放消息的容器,它支持生产者-消费者模式。
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;public class BlockingQueueExample {private final BlockingQueue<String> queue = new LinkedBlockingQueue<>(100);public void produce(String item) {try {queue.put(item); // 生产者放入消息} catch (InterruptedException e) {e.printStackTrace();}}public String consume() throws InterruptedException {return queue.take(); // 消费者取出消息}
}
4. Atomic 类
Java 提供了 java.util.concurrent.atomic
包来处理原子操作,可以用来实现线程安全的计数器或其他共享变量。
实现示例:
import java.util.concurrent.atomic.AtomicInteger;public class AtomicIntegerExample {private AtomicInteger count = new AtomicInteger(0);public int incrementAndGet() {return count.incrementAndGet();}
}
这些方法都可以帮助你在 Java 中实现线程间的通信。选择哪种方法取决于你的具体需求和上下文环境。