waynblog

三个线程交替打印ABC100次问题思考

如题:使用三个线程交替打印ABC,直至100次代码实战

方法一:

使用notify()、wait()方法

publicclassPrintAbc{

/**
     * 唤醒线程的状态值 state: threadA = 0, threadB = 1, threadC =2,
     */
int state =0;

/**
     * 循环技术,初始值0
     */
int count =0;

publicvoidprint(PrintAbc printAbc){
Thread threadA =newThread(()->{
            extracted(printAbc,"A",0,1);
});

Thread threadB =newThread(()->{
            extracted(printAbc,"B",1,2);
});

Thread threadC =newThread(()->{
            extracted(printAbc,"C",2,0);
});

        threadC.start();
        threadB.start();
try{
Thread.sleep(1000L);
}catch(InterruptedException e){
thrownewRuntimeException(e);
}
        threadA.start();
}

/**
     * 交替打印abc,直至100次
     *
     * @param printAbc  锁对象
     * @param a         打印的字母, 对应A、B、C
     * @param needState 当前线程对应的state状态值
     * @param nextState 唤醒下一个线程所需state状态值
     */
privatevoid extracted(PrintAbc printAbc,String a,int needState,int nextState){
while(true){
synchronized(printAbc){
if(count >=100){
break;
}
if(printAbc.count <100&& printAbc.state == needState){
System.out.println(a);
                    printAbc.state = nextState;
                    printAbc.count++;
                    printAbc.notifyAll();
}else{
try{
                        printAbc.wait();
}catch(InterruptedException e){
thrownewRuntimeException(e);
}
}
}
}
}

publicstaticvoid main(String[] args){
PrintAbc printAbc =newPrintAbc();
        printAbc.print(printAbc);
}
}

上述代码使用notify(),wait(),进行线程间的条件唤醒,state的初始状态是0,对应线程A,所以第一次打印字母也一定是A

方法二

使用ReentrantLock的的Condition条件

publicclassPrintAbcByCondition{

/**
     * 循环计数初始值0
     */
staticint count =0;

publicvoidprint(){
ReentrantLock reentrantLock =newReentrantLock();
Condition conditionA = reentrantLock.newCondition();
Condition conditionB = reentrantLock.newCondition();
Condition conditionC = reentrantLock.newCondition();
Thread threadA =newThread(()->{
while(true){
try{
                    reentrantLock.lock();
// threadA进来打印A然后唤醒threadB
if(count <100){
System.out.println("A");
                        count++;
                        conditionB.signal();
}
                    conditionA.await();

}catch(InterruptedException e){
thrownewRuntimeException(e);
}finally{
                    reentrantLock.unlock();
}
}
});
Thread threadB =newThread(()->{
while(true){
try{
                    reentrantLock.lock();
// threadB进来就阻塞等待threadA使用完毕
                    conditionB.await();
if(count <100){
System.out.println("B");
                        count++;
                        conditionC.signal();
}

}catch(InterruptedException e){
thrownewRuntimeException(e);
}finally{
                    reentrantLock.unlock();
}
}
});
Thread threadC =newThread(()->{
while(true){
try{
                    reentrantLock.lock();
// threadC进来就阻塞等待threadB使用完毕
                    conditionC.await();
if(count <100){
System.out.println("C");
                        count++;
                        conditionA.signal();
}
}catch(InterruptedException e){
thrownewRuntimeException(e);
}finally{
                    reentrantLock.unlock();
}
}
});

        threadC.start();
        threadB.start();
try{
Thread.sleep(1000L);
}catch(InterruptedException e){
thrownewRuntimeException(e);
}
        threadA.start();
}

publicstaticvoid main(String[] args){
newPrintAbcByCondition().print();
}
}

使用ReentrantLock的的Condition条件,很容易能实现三个线程之间的交替打印,需要注意的一点就是线程A是需要第一个执行,可以看到代码里threadA在等待1秒后在执行,也能确保是第一个进行打印,原因如下:

线程B和线程C中任意一个线程拿到锁都需要等待条件成立,线程C依赖线程B,而线程B依赖线程A,所以他们会一直阻塞直至线程A执行

上述两个方法中,核心问题就是如何实现线程间的条件唤醒,如方法一,我们可以自定义state状态变量来与各个线程绑定,每个线程都有自己对应的state状态,当state变量当前值与线程自身期望的state值相同才唤醒当前线程。也可以使用juc中ReentrantLock的提供的Condition条件完成线程间的条件唤醒

至此,三个线程交替打印ABC100次的实现方法介绍完毕