import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Condition实现线程间通信
* @author ETHAN
*
*/
public class ThreeConditionCommunicationTest {
public static void main(String[] args) {
final Business business = new Business();
new Thread(new Runnable() {
@Override
public void run() {
for(int i=1;i<=50;i++) {
business.sub2(i);
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
for(int i=1;i<=50;i++) {
business.sub3(i);
}
}
}).start();
for(int i=1;i<=50;i++) {
business.main(i);
}
}
static class Business {
//synchronized 用 lock替代
Lock lock = new ReentrantLock();
//老大
Condition condition1 = lock.newCondition();
//老二
Condition condition2 = lock.newCondition();
//老三
Condition
java 之 三个线程交替执行任务
最新推荐文章于 2023-03-29 23:13:11 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)