说明
最近碰到一个问题,使用三个线程交替打印 0-100 。主要考察多线程并发同步,锁的使用。这里记录下我用 Lock 和 多个 Condition 的实现方式。
在刚开始实现时,发现在数字输出完毕后,主线程无法停止,最后只能在判断满足条件后直接退出 JVM。这个原因是有线程在调用 await 方法后,没有被唤醒,导致线程没有正常结束。
所以,在每个线程执行完毕退出释放锁前,都要进行一次顺序唤醒操作。
还有一点需要注意的是,尽量不要在循环中使用 try catch 语句。
正文
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class PrintNum {
public static void main(String[] args) {
Lock lock = new ReentrantLock();
Condition c1 = lock.newCondition();
Condition c2 = lock.newCondition();
Condition c3 = lock.newCondition();
AtomicInteger count = new AtomicInteger(0);
Runnable task1 = new Runnable() {
@Override
public void run() {
try {
lock.lock();
while (true) {
if (count.get() > 100) {
return;
}
System.out.println(Thread.currentThread().getName() + "-" + count.getAndAdd(1));
c2.signal();
c1.await();
}
} catch (Exception e) {
} finally {
c2.signal();
lock.unlock();
}
}
};
Runnable task2 = new Runnable() {
@Override
public void run() {
try {
lock.lock();
while (true) {
if (count.get() > 100)
return;
System.out.println(Thread.currentThread().getName() + "-" + count.getAndAdd(1));
c3.signal();
c2.await();
}
} catch (Exception e) {
} finally {
c3.signal();
lock.unlock();
}
}
};
Runnable task3 = new Runnable() {
@Override
public void run() {
try {
lock.lock();
while (true) {
if (count.get() > 100)
return;
System.out.println(Thread.currentThread().getName() + "-" + count.getAndAdd(1));
c1.signal();
c3.await();
}
} catch (Exception e) {
} finally {
c1.signal();
lock.unlock();
}
}
};
Thread t1 = new Thread(task1, "A");
t1.start();
Thread t2 = new Thread(task2, "B");
t2.start();
Thread t3 = new Thread(task3, "C");
t3.start();
try {
System.out.println("任务线程已启动");
t1.join();
t2.join();
t3.join();
System.out.println("线程执行完毕");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}