java多线程循环打印数列

用Java多线程实现循环打印数字

方式一:使用java的synchronized锁实现

public class LoopPrint implements Runnable {
    public static volatile AtomicInteger totalThread = new AtomicInteger(0);
    public static volatile AtomicInteger point = new AtomicInteger(1);
    private static final  Object lockObj = new Object();
    private int id;
    public LoopPrint(int id,int total) {
        totalThread.set(total);
        this.id = id;
    }
    @Override
    public void run() {
        while (true){
            synchronized (lockObj){
                int i = point.get();
                if (i==id){
                    System.out.println("线程"+this.id+":当前值为:"+i);
                    lockObj.notify();
                    if (i == totalThread.get()){
                        point.set(1);
                    }else{
                        point.addAndGet(1);
                    }
                    try {
                        lockObj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    public static void main(String[] args) {
       new Thread( new LoopPrint(1,9)).start();
        new Thread( new LoopPrint(2,9)).start();
        new Thread( new LoopPrint(3,9)).start();
        new Thread( new LoopPrint(4,9)).start();
        new Thread( new LoopPrint(5,9)).start();
        new Thread( new LoopPrint(6,9)).start();
        new Thread( new LoopPrint(7,9)).start();
        new Thread( new LoopPrint(8,9)).start();
        new Thread( new LoopPrint(9,9)).start();
    }

}

方式二:使用JUC包中的lock及Condition实现

public class LoopPrint3 implements Runnable{
    private  static  ReentrantLock lock = new ReentrantLock();
    private static  AtomicInteger total = new AtomicInteger(0);
    private  static  List<Condition> conditions = new ArrayList<>();
    private static volatile  int point = 0;
    private int id;
    public LoopPrint3() {
        conditions.add(lock.newCondition());
        this.id = total.getAndAdd(1);
    }
    @Override
    public void run() {
       try {
           lock.lock();
           while (true){
               if (point != id){
                   conditions.get(id).await();
               }
               System.out.println(Thread.currentThread().getName()+":"+(++point));
               if (point ==total.get() ){
                   point = 0;
               }
               if (id == total.get()-1){
                   conditions.get(0).signal();
               }else{
                   conditions.get(id+1).signal();
               }
           }
       } catch (InterruptedException e) {
           e.printStackTrace();
       } finally {
           lock.unlock();
       }
    }

    public static void main(String[] args) {
        new Thread(new LoopPrint3()).start();
        new Thread(new LoopPrint3()).start();
        new Thread(new LoopPrint3()).start();
        new Thread(new LoopPrint3()).start();
        new Thread(new LoopPrint3()).start();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值