• 生产者和消费者关系


    package cn.ljs.FristSync;
    
    
    import java.util.ArrayList;
    
    public class ProductorDemo {
        
        public static void main(String[] args) {
            
            Pool pool = new Pool();
            Productor01 productor01 = new Productor01("productor1", pool);
            Productor01 productor02 = new Productor01("productor2", pool);
            Comsumer01 comsumer01 = new Comsumer01("comsumer1", pool);
            Comsumer01 comsumer02 = new Comsumer01("comsume2", pool);
            
            
            productor01.start();
            productor02.start();
            comsumer01.start();
            comsumer02.start();
            
        }
    }
    
    
    class Productor01 extends Thread{
        private String name;
        private Pool pool;
        private static int i=1;
        
        public Productor01(String name, Pool pool){
            this.name = name;
            this.pool = pool;
        }
        
        public void run(){
            while (true) {
                pool.add(i);
                System.out.println(name + " add: " + i);
                i++;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    class Comsumer01 extends Thread{
        private String name;
        private Pool pool;
        
        public Comsumer01(String name, Pool pool){
            this.name = name;
            this.pool = pool;
        }
        
        public void run(){
            while (true) {
                int n = pool.remove();
                System.out.println(name + " remove: " + n);
            }
        }
    }
    
    class Pool{
        ArrayList<Integer> list = new ArrayList<Integer>();
        private int Max =100;
        public void add(int n){    
            synchronized (this) {
                
                try {
                    while( list.size() >= Max) {
                        this.wait();
                    }
                    list.add(n);
                    this.notify();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        public int remove() {
            synchronized (this) {
                try {
                    while( list.size() == 0 ){
                        this.wait();
                    }
                    int n = list.remove(0);
                    this.notify();
                    return n;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return -1;
        }
    }
  • 相关阅读:
    广播与服务知识点总结
    Intent和Activity知识点总结
    数据库基础
    Java 中JOptionPane的基本使用方法
    Eclipse 自动补全功能失效解决办法及修改快捷键方法
    hdu 2095 find your present (2)
    sort()
    qsort()
    算法学习——分治算法
    NYOJ——街区最短路径问题
  • 原文地址:https://www.cnblogs.com/lijins/p/10057248.html
Copyright © 2020-2023  润新知