【多线程】一些小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();

    }
}
全部评论

相关推荐

10-07 20:48
门头沟学院 Java
不敢追175女神:可能是实习上着班想到后面还要回学校给导师做牛马,看着身边都是21-25的年纪,突然emo了了
点赞 评论 收藏
分享
评论
点赞
1
分享
牛客网
牛客企业服务