zoukankan      html  css  js  c++  java
  • Java线程实现提供者消费者模式

    package com.hundsun.hswealth.mkm;
    
    public class MainApp {
    
        private static Object lock1 = new Object(); // 锁对象
        private static volatile boolean status = true; // 共享变量可见性
    
        static class SubThread1 extends Thread {
            @Override
            public void run() {
                synchronized (lock1) {
                    while (status) {
                        System.out.println(Thread.currentThread().getName() + "提供者提供商品...");
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        status = false;
                    }
    
                    System.out.println(Thread.currentThread().getName() + "等待消费者消费商品...");
                    lock1.notifyAll(); // 通知所有线程
                }
            }
        }
    
        static class SubThread2 extends Thread {
            @Override
            public void run() {
                synchronized (lock1) {
                    while (!status) {
                        System.out.println(Thread.currentThread().getName() + "消费者消费商品...");
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        status = true;
                    }
                    System.out.println(Thread.currentThread().getName() + "等待提供者提供商品...");
                    try {
                        lock1.wait(); // 线程等待
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        public static void main(String[] args) {
    
            for (;;) {
                Thread thread1 = new Thread(new SubThread1());
                Thread thread2 = new Thread(new SubThread2());
                thread1.start();
                thread2.start();
            }
        }
    }
  • 相关阅读:
    5_添加购物车 View+Con
    5_添加购物车 B+M
    5_添加购物车 D
    登录注册V
    bootstrap-标题
    h5整理--详解css的相对定位和绝对定位
    各大门户网站的css初始化代码
    九月二十八JS验证
    js函数和运算符
    4.1原始表达式 9/16
  • 原文地址:https://www.cnblogs.com/w1440199392/p/15723634.html
Copyright © 2011-2022 走看看