java线程如何同步和通讯
Java线程同步与通信的方法
使用synchronized关键字
synchronized是Java中最基础的线程同步机制,可以修饰方法或代码块。当一个线程进入synchronized方法或代码块时,其他线程必须等待当前线程执行完毕才能进入。
public class Counter {
private int count = 0;
public synchronized void increment() {
count++;
}
}
使用volatile关键字
volatile保证变量的可见性,即当一个线程修改了volatile变量的值,其他线程能立即看到最新值。但不保证原子性,适合单个变量的简单同步场景。
public class VolatileExample {
private volatile boolean flag = false;
}
使用Lock接口
Java.util.concurrent.locks包提供了更灵活的锁机制,如ReentrantLock。相比synchronized,Lock提供了更多功能,如尝试获取锁、定时锁等待等。
Lock lock = new ReentrantLock();
public void method() {
lock.lock();
try {
// 临界区代码
} finally {
lock.unlock();
}
}
使用wait()/notify()机制
这是Java内置的线程间通信机制。wait()使线程等待并释放锁,notify()/notifyAll()唤醒等待的线程。必须在synchronized块中使用。

public synchronized void produce() throws InterruptedException {
while (queue.isFull()) {
wait();
}
// 生产数据
notifyAll();
}
使用BlockingQueue
java.util.concurrent.BlockingQueue是线程安全的队列,提供了put()/take()等阻塞方法,天然适合生产者-消费者模式。
BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(10);
// 生产者
queue.put(item);
// 消费者
Integer item = queue.take();
使用CountDownLatch
CountDownLatch允许一个或多个线程等待其他线程完成操作。初始化时指定计数,countDown()减少计数,await()阻塞直到计数为0。
CountDownLatch latch = new CountDownLatch(3);
// 工作线程
latch.countDown();
// 主线程
latch.await();
使用CyclicBarrier
CyclicBarrier让一组线程互相等待,到达屏障点时才能继续执行。与CountDownLatch不同,它可以重置重用。

CyclicBarrier barrier = new CyclicBarrier(3, () -> {
// 所有线程到达后执行
});
// 线程中
barrier.await();
使用Semaphore
Semaphore控制同时访问特定资源的线程数量,通过acquire()获取许可,release()释放许可。
Semaphore semaphore = new Semaphore(5);
semaphore.acquire();
try {
// 访问资源
} finally {
semaphore.release();
}
使用Exchanger
Exchanger允许两个线程在同步点交换数据,适用于校对工作等场景。
Exchanger<String> exchanger = new Exchanger<>();
// 线程1
String data2 = exchanger.exchange(data1);
// 线程2
String data1 = exchanger.exchange(data2);
使用Future和Callable
通过ExecutorService提交Callable任务,返回Future对象,可以获取异步计算结果。
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<Integer> future = executor.submit(() -> {
return 1 + 1;
});
Integer result = future.get();
选择合适的方法取决于具体场景:简单同步用synchronized,复杂锁需求用Lock,线程协作用wait/notify或并发集合,任务协调用CountDownLatch/CyclicBarrier,资源控制用Semaphore。






