跳转到主内容
新派编程网 - 新潮编程理念,全新技术课堂!

使用 volatile 变量控制多线程交替打印「FooBar」时如何避免死锁?

使用两个 volatile 变量控制多线程时死锁问题 题目要求设计一个多线程程序交替打印「foobar」。开发者按照题意使用了两个 volatile 变量控制多线程逻辑,但运行后程序卡死在 while 循环中。 原因分析 使用 volatile 变量确实可以防止指令重排序,但它不能解决线程 并发访问 同一片段内存区域时可能出现的共享变量竞争问题。while 循环中的 busy-wait 代码导致线程一直占用 cpu 资源,无法释放锁,从而造成死锁。 解决方案 为了解决死锁问题,可以使用 wait() 和 notify()/notifyall() 来实现线程之间的协调,从而避免忙等待。 修改后的代码如下:
class FooBar { private int n; private boolean flag = false; public FooBar(int n) { this.n = n; } public synchronized void foo(Runnable printFoo) throws InterruptedException { for (int i = 0; i < n; i++) { while (flag) { wait(); } // printFoo.run() outputs "foo". Do not change or remove this line. printFoo.run(); flag = true; notifyAll(); } } public synchronized void bar(Runnable printBar) throws InterruptedException { for (int i = 0; i < n; i++) { while (!flag) { wait(); } // printBar.run() outputs "bar". Do not change or remove this line. printBar.run(); flag = false; notifyAll(); } } }
在修改后的代码中,使用 synchronized 关键字保证了 foo 和 bar 方法的互斥执行。线程在等待条件满足时调用 wait() 方法来等待,并释放锁,让其他线程有机会执行。当条件满足时,线程会调用 notifyall() 方法来唤醒其他等待的线程,从而避免死锁。

相关文章