� 《Java线程大揭秘:如何轻松实现线程交替输出?

在Java编程中,实现线程交替输出是一个常见的并发编程问题。它要求多个线程按照一定的顺序交替执行,并输出特定的信息。这种模式在多线程同步和线程间通信方面提供了很好的实践机会。本文将详细介绍如何在Java中实现线程交替输出。
线程交替输出的基本原理
线程交替输出通常涉及到以下几个关键点:
共享资源
:多个线程共享一个资源,如一个
Object
实例或
CountDownLatch
。
同步机制
:使用
Synchronized
关键字或其他同步工具(如
ReentrantLock
、
Semaphore
等)来保证线程间的互斥访问。
线程间通信
:通过
wait()
和
notify()
方法,或者使用
Condition
接口来实现线程间的通信。
实现线程交替输出的方法
以下是一些实现线程交替输出的常见方法:
方法一:使用
synchronized
和
wait()
、
notify()
方法
class PrintTask implements Runnable {
private int count;
private final Object lock;

public PrintTask(int count, Object lock) {
this.count = count;
this.lock = lock;
}

@Override
public void run() {
for (int i = 0; i < count; i++) { synchronized (lock) { lock.notify(); try { lock.wait(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } System.out.println(Thread.currentThread().getName() + " - " + i); } } } public class Thread交替输出示例 { public static void main(String[] args) { Object lock = new Object(); Thread thread1 = new Thread(new PrintTask(10, lock), "Thread-1"); Thread thread2 = new Thread(new PrintTask(10, lock), "Thread-2"); thread1.start(); thread2.start(); } } 方法二:使用 ReentrantLock 和 Condition import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; class PrintTask implements Runnable { private int count; private final ReentrantLock lock; private final Condition condition; public PrintTask(int count, ReentrantLock lock, Condition condition) { this.count = count; this.lock = lock; this.condition = condition; } @Override public void run() { for (int i = 0; i < count; i++) { lock.lock(); try { condition.signal(); condition.await(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { lock.unlock(); } System.out.println(Thread.currentThread().getName() + " - " + i); } } } public class Thread交替输出示例 { public static void main(String[] args) { ReentrantLock lock = new ReentrantLock(); Condition condition = lock.newCondition(); Thread thread1 = new Thread(new PrintTask(10, lock, condition), "Thread-1"); Thread thread2 = new Thread(new PrintTask(10, lock, condition), "Thread-2"); thread1.start(); thread2.start(); } } 方法三:使用 Semaphore import java.util.concurrent.Semaphore; class PrintTask implements Runnable { private int count; private final Semaphore semaphore; public PrintTask(int count, Semaphore semaphore) { this.count = count; this.semaphore = semaphore; } @Override public void run() { for (int i = 0; i < count; i++) { try { semaphore.acquire(); System.out.println(Thread.currentThread().getName() + " - " + i); semaphore.release(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } } public class Thread交替输出示例 { public static void main(String[] args) { Semaphore semaphore = new Semaphore(1); Thread thread1 = new Thread(new PrintTask(10, semaphore), "Thread-1"); Thread thread2 = new Thread(new PrintTask(10, semaphore), "Thread-2"); thread1.start(); thread2.start(); } } 总结 通过以上方法,我们可以轻松实现Java线程的交替输出。选择合适的方法取决于具体的应用场景和需求。在实际开发中,合理地使用线程同步和通信机制对于构建高效、稳定的并发程序至关重要。