多线程-07-算法面试题--02--手写交替输出

题目描述:有三个线程,交替输出A、B、C,各输出5次

一、Synchronized实现(wait/notifyAll)

package com.fuping3.printAsOrder;

public class PrintCharWithSyn {
    private int stat=0;

    public void printChar(String str,int waitFlag,int nextFlag){
        synchronized (this){
            while(stat!=waitFlag){
                try {
                    //System.out.println(Thread.currentThread().getName()+"阻塞");
                    this.wait(1000*5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName()+":"+str);
            stat=nextFlag;
            this.notifyAll();
        }
    }

}
//测试代码
public static void main(String[] args) throws InterruptedException {
        PrintCharWithSyn pt=new PrintCharWithSyn();
        new Thread(()->{
            for(int i=0;i<5;i++){
                pt.printChar("A",0,1);
            }
        },"t1").start();
        new Thread(()->{
            for(int i=0;i<5;i++){
                pt.printChar("B",1,2);
            }
        },"t2").start();
        new Thread(()->{
            for(int i=0;i<5;i++){
                pt.printChar("C",2,0);
            }
        },"t3").start();


    }

二、Lock实现(await/signal)

package com.fuping3.printAsOrder;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class PrintCharWithLock extends ReentrantLock{


    public void printChar(String str,Condition curr,Condition next){
        lock();
        try{
            curr.await();
            System.out.println(Thread.currentThread().getName()+":"+str);
            next.signal();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            unlock();
        }
    }
}
//测试代码
public static void runWithLock() throws InterruptedException {
        PrintCharWithLock ptLock=new PrintCharWithLock();
        Condition c1=ptLock.newCondition();
        Condition c2=ptLock.newCondition();
        Condition c3=ptLock.newCondition();

        new Thread(()->{
            for(int i=0;i<5;i++){
                ptLock.printChar("A",c1,c2);
            }
        },"t1").start();
        new Thread(()->{
            for(int i=0;i<5;i++){
                ptLock.printChar("B",c2,c3);
            }
        },"t2").start();
        new Thread(()->{
            for(int i=0;i<5;i++){
                ptLock.printChar("C",c3,c1);
            }
        },"t3").start();

        /**
         * 1、各线程进入后立即进入等待室,所以主线程需要唤醒c1
         * 2、主线程需要等待一段时间,使各线程能进入等待室
         */
        Thread.sleep(1000);
        ptLock.lock();
        try{
            c1.signal();
        }finally {
            ptLock.unlock();
        }
    }

三、park/unpark实现

注意:

1、park/unpark此处用不到锁;

2、线程启动以后,park/unpark执行顺序无影响

package com.fuping3.printAsOrder;

import java.util.concurrent.locks.LockSupport;

public class PrintCharWithPark {

    public void printChar(String str,Thread nextThread){
        LockSupport.park();
        System.out.println(Thread.currentThread().getName()+":"+str);
        LockSupport.unpark(nextThread);
    }
}
//测试代码
public class TestWithSynOrLock {
    private static Thread t1,t2,t3;
    public static void main(String[] args) throws InterruptedException {
        PrintCharWithPark pt=new PrintCharWithPark();
        t1=new Thread(()->{
            for(int i=0;i<5;i++){
                pt.printChar("A",t2);
            }
        },"t1");
        t2=new Thread(()->{
            for(int i=0;i<5;i++){
                pt.printChar("B",t3);
            }
        },"t2");
        t3=new Thread(()->{
            for(int i=0;i<5;i++){
                pt.printChar("C",t1);
            }
        },"t3");



        t1.start();
        t2.start();
        t3.start();
        LockSupport.unpark(t1);



    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值