面试经典题目:多线程循环打印

写法1:lock

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

public class UsingLock {
    Lock lock=new ReentrantLock();  // 保证每次只有一个线程能够拿到资源
    int time; // 控制打印次数
    int state=0; // 当前状态值:保证三个线程之间交替打印
    public UsingLock(int time) {
        this.time = time;
    }
    private void Print(String str,int flag){
        for(int i=0;i<time;){
            lock.lock();
            //锁代码区
            if(state%3==flag){
                i++;
                state++;
                System.out.println("线程"+str+",第"+i+"次打印");
            }
            lock.unlock();
        }
    }
    private void PrintA(){
        Print("A",0);
    }
    private void PrintB(){
        Print("B",1);
    }
    private void PrintC(){
        Print("C",2);
    }

    public static void main(String[] args) {
        UsingLock test=new UsingLock(10);
        //启动多个线程
        new Thread(test::PrintA).start();
        new Thread(test::PrintB).start();
        new Thread(test::PrintC).start();
    }
}

 

写法2:Usingsemaphore

import java.util.concurrent.Semaphore;

public class Usingsemaphore {
    private int time;
    private int num=0;
    private char ch='a';
    private char chA='A';
    private Semaphore a=new Semaphore(1);
    private Semaphore b=new Semaphore(0);
    private Semaphore c=new Semaphore(0);
    public Usingsemaphore(int time) {
        this.time = time;
    }
    private void PrintA(){
        try{
           Print("A",a,b);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private void PrintB(){
        try{
            Print("B",b,c);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private void PrintC(){
        try{
            Print("C",c,a);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private void Print(String str,Semaphore current,Semaphore next){
        try{
            for(int i=0;i<time;i++){
                current.acquire();
                if(str.compareTo("A")==0){
                    System.out.print(num);
                    num++;
                }else if(str.compareTo("B")==0){
                    System.out.print(ch);
                    ch+=1;
                }else{
                    System.out.print(chA);
                    chA+=1;
                }
                next.release();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Usingsemaphore test=new Usingsemaphore(10);
        new Thread(test::PrintA).start();
        new Thread(test::PrintB).start();
        new Thread(test::PrintC).start();
    }
}

写法3:Wait Notify synchronized

public class Usingwaitnotify {
    int time;
    int state=0;
    private Object a = new Object();
    private Object b = new Object();
    private Object c = new Object();
    public Usingwaitnotify(int time) {
        this.time = time;
    }

    private void Print(String str,int flag,Object a,Object b) throws Exception{
        for(int i=0;i<time;){
            synchronized (a){
                while(state%3!=flag){
                    a.wait();
                }
                state++;
                i++;
                System.out.println("线程:"+str+",第"+i+"次打印");
                synchronized (b){
                    b.notify();
                }
            }
        }
    }
    private void PrintA(){
        try{
            Print("A",0,a,b);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private void PrintB(){
        try{
            Print("B",1,b,c);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private void PrintC(){
        try{
            Print("C",2,c,a);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Usingwaitnotify test=new Usingwaitnotify(10);
        new Thread(test::PrintA).start();
        new Thread(test::PrintB).start();
        new Thread(test::PrintC).start();
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值