【多线程】一些小demo

交替打印ABC

ReentrantLock() 方式

public class PrintABC {

    static Lock lock = new ReentrantLock();
    static Condition A = lock.newCondition();
    static Condition B = lock.newCondition();
    static Condition C = lock.newCondition();
    static volatile int cnt = 0;
    public static void main(String[] args) {
        new Thread(() ->{
            try {
                lock.lock();
                for(int i = 1; i <= 10; i ++){
                    while (cnt % 3 != 0)A.await();//当cnt != 0 时,说明该其他线程执行
                    System.out.println(Thread.currentThread().getName() + "第" + i + "次打印A" );
                    cnt ++;
                    B.signal();//唤醒B
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        },"线程A").start();
        new Thread(() ->{
            try {
                lock.lock();
                for(int i = 1; i <= 10; i ++){
                    while (cnt % 3 != 1)B.await();//当cnt != 1 时,说明该其他线程执行
                    System.out.println(Thread.currentThread().getName() + "第" + i + "次打印B" );
                    cnt ++;
                    C.signal();//唤醒C
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        },"线程B").start();
        new Thread(() ->{
            try {
                lock.lock();
                for(int i = 1; i <= 10; i ++){
                    while (cnt % 3 != 2)C.await();//当cnt != 2 时,说明该其他线程执行
                    System.out.println(Thread.currentThread().getName() + "第" + i + "次打印C");
                    cnt ++;
                    A.signal();//唤醒A
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        },"线程C").start();
    }
}

交替打印奇偶

public class PrintOdd {
    private static final int max = 100;
    private static AtomicInteger i = new AtomicInteger(0);
    public static void main(String[] args)  {

        new Thread(() ->{
            while (i.get() <= max) {
                if(i.get() % 2 == 0){
                    System.out.println(Thread.currentThread().getName() + ":" + i);
                    i.incrementAndGet();
                }
            }

        }).start();

        new Thread(() ->{
            while (i.get() <  max) {
                if(i.get() % 2 == 1){
                    System.out.println(Thread.currentThread().getName() + ":" + i);
                    i.incrementAndGet();
                }
            }

        }).start();
    }
}

死锁

public class DeadLock {
    private static Object A = new Object();
    private static Object B = new Object();

    public static void main(String[] args) {

        new Thread(() ->{
            synchronized (A){

                synchronized (B){
                    System.out.println("--A--");
                }
            }
        },"A").start();

        new Thread(() ->{
            synchronized (B){
                synchronized (A){
                    System.out.println("--B--");
                }
            }
        },"B").start();

    }
}
全部评论

相关推荐

11-01 20:03
已编辑
门头沟学院 算法工程师
Amazarashi66:这种也是幸存者偏差了,拿不到这个价的才是大多数
点赞 评论 收藏
分享
尊嘟假嘟点击就送:加v细说,问题很大
点赞 评论 收藏
分享
评论
点赞
1
分享
牛客网
牛客企业服务