zoukankan      html  css  js  c++  java
  • java 线程间的通信 (wait / notify / notifyAll)

    package waitnotifytest;
    
    import java.util.Collections;
    import java.util.List;
    import com.google.common.collect.Lists;
    
    /**
     * Test
     */
    public class Test {
    
        private static final Class<Test> lockObj = Test.class;
    
        private final List<String> list = Collections.synchronizedList(Lists.newArrayListWithCapacity(10));
    
        public static void main(String[] args) {
            Test test = new Test();
            test.test();
        }
    
        private void push() {
            while (list.size() >= 10) {
                System.out.println("厨师:" + Thread.currentThread().getName() + " 等待中...");
                try {
                    synchronized (lockObj) {
                        lockObj.wait();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            list.add("item");
            list.add("item");
            list.add("item");
            list.add("item");
            list.add("item");
            System.out.println("出菜台的菜品数量" + list.size());
            synchronized (lockObj) {
                System.out.println("厨师通知所有人");
                lockObj.notifyAll();
            }
        }
    
        private void popup() {
            synchronized (lockObj) {
                while (list.isEmpty()) {
                    try {
                        System.out.println("服务员:" + Thread.currentThread().getName() + "等待中...");
                        lockObj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                list.remove(list.size() - 1);
            }
            System.out.println("出菜口的菜品数量为:" + list.size());
            synchronized (lockObj) {
                lockObj.notifyAll();
            }
            System.out.println("服务员通知所有人...");
        }
    
        public void test() {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        push();
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
    
            Runnable runnable2 = new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        popup();
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
    
            for (int i = 0; i < 2; i++) {
                Thread thread = new Thread(runnable);
                thread.start();
    
            }
            for (int i = 0; i < 10; i++) {
                Thread thread = new Thread(runnable2);
                thread.start();
            }
    
        }
    }
  • 相关阅读:
    Appium1.6启动ios9.3报错Original error: Sdk '9.3.5' was not in list of simctl sdks
    Appium,安装WebDriverAgent(WDA)
    Charles界面介绍及使用方法
    手机通过Charles抓取https包
    jacoco统计server端功能测试覆盖率
    Android学习路-activity活动
    Android学习路-Android Studio的工程目录
    js实现多级复选框的交互
    python3使用paramiko操作远程机器
    ubuntu中查看各种设备和资源的命令汇总
  • 原文地址:https://www.cnblogs.com/frankyou/p/10729564.html
Copyright © 2011-2022 走看看