zoukankan      html  css  js  c++  java
  • 2个线程ABAB或者BABA循环输出

    代码:

    /**
     * 两个线程循环打印输出a-b-a-b
     */
    public class AandBforTOthread {
        private static Object o = new Object();
        private static int flag=0;
        private static boolean close=false;
        static class A implements Runnable {
            @Override
            public void run() {
                synchronized (o) {
                    if (flag==0){
                        flag=1;
                    }
                    while (!close) {
                        System.out.println("A");
                        if (flag==1){
                            try {
                                o.wait();//释放锁
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            o.notify();
                        }else {
                            o.notify();
                            try {
                                o.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
    
                    }
                    o.notify();
                }
            }
        }
    
        static class B implements Runnable {
            @Override
            public void run() {
                if (flag==0){
                    flag=2;
                }
                synchronized (o) {
                    while (!close) {
                        System.out.println("B");
                        if (flag==1){
                            o.notify();
                            try {
                                o.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }else{
                            try {
                                o.wait();//释放锁
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            o.notify();
                        }
                    }
                    o.notify();
                }
            }
        }
    
        public static void main(String[] args) {
            Thread a = new Thread(new A());
            Thread b = new Thread(new B());
            b.start();
            a.start();
            try {
                Thread.sleep(10l);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            close=true;
            a.interrupt();
            b.interrupt();
        }
    }
    

      输出:

  • 相关阅读:
    leetcode 1. 两数之和
    leetcode 671. 二叉树中第二小的节点
    leetcode 100. 相同的树
    leetcode 110. 平衡二叉树
    leetcode 144. 二叉树的前序遍历
    1066. Root of AVL Tree (25)
    leetcode 100 相同的树
    leeCode 515 在每个树行中找最大值
    LeetCode 31.下一个排列
    面向对象UML中类关系
  • 原文地址:https://www.cnblogs.com/qfxydtk/p/8728877.html
Copyright © 2011-2022 走看看