多线程交替打印ABC的多种方式

使用lock,Condition

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

public class ABC {

    //可重入锁
    private final static Lock lock = new ReentrantLock();
    //判断是否执行:1表示应该A执行,2表示应该B执行,3表示应该C执行
    private static int state = 1;
    //condition对象
    private static Condition a = lock.newCondition();
    private static Condition b = lock.newCondition();
    private static Condition c = lock.newCondition();

    public static void printA() {
        //通过循环,hang住线程
        for (int i = 0; i < 10; i++) {
            try {
                //获取锁
                lock.lock();
                //并发情况下,不能用if,要用循环判断等待条件,避免虚假唤醒
                while (state != 1) {
                    a.await();
                }
                System.out.print("A");
                state = 2;
                b.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                //要保证不执行的时候,锁能释放掉
                lock.unlock();
            }
        }
    }

    public static void printB() throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            try {
                lock.lock();
                //获取到锁,应该执行
                while (state != 2) {
                    b.await();
                }
                System.out.print("B");
                state = 3;
                c.signal();
            } finally {
                lock.unlock();
            }
        }
    }

    public static void printC() throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            try {
                lock.lock();
                while (state != 3) {
                    c.await();
                }
                //获取到锁,应该执行
                System.out.print("C");
                state = 1;
                a.signal();
            } finally {
                lock.unlock();
            }
        }
    }


    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                ABC.printA();
            }
        }, "A").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    ABC.printB();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "B").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    ABC.printC();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "C").start();
    }

}

使用AtomicInteger

import java.util.concurrent.atomic.AtomicInteger;

public class ABC2 {
    private static AtomicInteger state = new AtomicInteger(1);

    public static void printA() {
        for (int i = 0; i < 10; i++) {
            while (true) {
                if (state.get() == 1) {
                    System.out.print("A");
                    state.incrementAndGet();
                    break;
                }
            }
        }
    }

    public static void printB() {
        for (int i = 0; i < 10; i++) {
            while (true) {
                if (state.get() == 2) {
                    System.out.print("B");
                    state.incrementAndGet();
                    break;
                }
            }
        }
    }

    public static void printC() {
        for (int i = 0; i < 10; i++) {
            while (true) {
                if (state.get() == 3) {
                    System.out.print("C");
                    state.set(1);
                    break;
                }
            }
        }
    }

    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                ABC2.printA();
            }
        }, "A").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                ABC2.printB();
            }
        }, "B").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                ABC2.printC();
            }
        }, "C").start();
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值