原 lock绑定多条件Condition
版权声明:本文为博主原创文章,请尊重他人的劳动成果,转载请附上原文出处链接和本声明。
本文链接:https://www.91mszl.com/zhangwuji/article/details/1146
多线程之间按顺序调用,实现A->B->C三个线程启动,要求A打印5次,B打印10次,C打印15次,ABC都循环10次
代码如下:
package com.mszl.thread;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* 功能:锁绑定多个条件:多线程之间按顺序调用,实现A->B->C三个线程启动,要求A打印5次,B打印10次,C打印15次,ABC都循环10次
* 备注:更多资料请访问 http://www.91mszl.com
* @author bobo teacher
*/
class shareData{
private int number=1; // 为1是A打印,为2时B打印,为3时C打印
private Lock lk=new ReentrantLock();
private Condition cd1=lk.newCondition();
private Condition cd2=lk.newCondition();
private Condition cd3=lk.newCondition();
public void print5(){
lk.lock();
try{
// 1 判断
while(number!=1){ // A==1时开始打印
cd1.await();
}
// 2 业务逻辑
for(int i=1; i<=5; i++){
System.out.println(Thread.currentThread().getName() + "\t" + number);
}
// 3 通知
number=2;
cd2.signal();
} catch(Exception ex){
ex.printStackTrace();
} finally{
lk.unlock();
}
}
public void print10(){
lk.lock();
try{
// 1 判断
while(number!=2){ // B==2时开始打印
cd2.await();
}
// 2 业务逻辑
for(int i=1; i<=10; i++){
System.out.println(Thread.currentThread().getName() + "\t" + number);
}
// 3 通知
number=3;
cd3.signal();
} catch(Exception ex){
ex.printStackTrace();
} finally{
lk.unlock();
}
}
public void print15(){
lk.lock();
try{
// 1 判断
while(number!=3){ // C==3时开始打印
cd3.await();
}
// 2 业务逻辑
for(int i=1; i<=15; i++){
System.out.println(Thread.currentThread().getName() + "\t" + number);
}
// 3 通知
number=1;
cd1.signal();
} catch(Exception ex){
ex.printStackTrace();
} finally{
lk.unlock();
}
}
}
public class MoreConditionDemo {
public static void main(String[] args) {
shareData sd=new shareData();
// A线程
Thread t1=new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <=10; i++) {
sd.print5();
}
}
});
t1.start();
// B线程
Thread t2=new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <=10; i++) {
sd.print10();
}
}
});
t2.start();
// C线程
Thread t3=new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <=10; i++) {
sd.print15();
}
}
});
t3.start();
}
}
执行结果:
Thread-0 1
Thread-0 1
Thread-0 1
Thread-0 1
Thread-0 1
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-0 1
Thread-0 1
Thread-0 1
Thread-0 1
Thread-0 1
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-0 1
Thread-0 1
Thread-0 1
Thread-0 1
Thread-0 1
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-0 1
Thread-0 1
Thread-0 1
Thread-0 1
Thread-0 1
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-0 1
Thread-0 1
Thread-0 1
Thread-0 1
Thread-0 1
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-0 1
Thread-0 1
Thread-0 1
Thread-0 1
Thread-0 1
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-0 1
Thread-0 1
Thread-0 1
Thread-0 1
Thread-0 1
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-0 1
Thread-0 1
Thread-0 1
Thread-0 1
Thread-0 1
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-0 1
Thread-0 1
Thread-0 1
Thread-0 1
Thread-0 1
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-0 1
Thread-0 1
Thread-0 1
Thread-0 1
Thread-0 1
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-1 2
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
Thread-2 3
2019-12-02 14:30:49 阅读(1183)
名师出品,必属精品 https://www.91mszl.com
博主信息