zoukankan      html  css  js  c++  java
  • java 11-10的test

    class TenAnianm4{
    public static void main(String[] agrs) {
    Pool pool=new Pool();
    Bear b = new Bear("B1",pool);
    //Bear b1 = new Bear("B2",pool);
    Cat c = new Cat("C",pool);
    b.start();
    //b1.start();
    c.start();

    }

    }

    class Pool {
    private int Max=20;
    private java.util.List<Integer> list =new java.util.LinkedList<Integer>();
    public void addMI(int i){
    synchronized (this ){
    while (list.size()>=Max){
    try{
    this.wait(2000);
    }
    catch(Exception e){

    }

    }

    list.add(new Integer(i));
    this.notifyAll();
    }
    }

    public int Remove() {
    synchronized (this ){
    while ((list.size())<Max){
    try{
    this.wait(2000);
    }
    catch(Exception e){

    }
    }
    this.notifyAll();
    list.clear();
    return list.size();
    }
    }
    }

    class Bear extends Thread{
    static int i=1;
    private String name;
    private Pool pool;
    public Bear(String name,Pool pool){
    this.name=name;
    this.pool=pool;
    }

    public void run(){
    while(true){
    pool.addMI(i);
    System.out.println("生产了=====》》" + i +"g");
    i++;
    }
    }

    }


    class Cat extends Thread{
    private String name;
    private Pool pool;
    public Cat(String name,Pool pool){
    this.name=name;
    this.pool=pool;
    }

    public void run(){
    while(true){
    pool.Remove();
    System.out.println("吃掉了==》》");
    }
    }

    }

  • 相关阅读:
    提高SQL查询效率
    数据库主键设计之思考
    Hlg 1030 排序
    Hdu 1556 成段更新.cpp
    Hdu 4280 最大流<模板>.cpp
    POJ 3216 最短路径匹配+floyd
    Hdu 4268 multiset函数的应用
    ZOJ 3602 树的同构
    Hdu 4284 状态DP 能否走完所选城市.cpp
    Hlg 1481 二分图匹配+二分.cpp
  • 原文地址:https://www.cnblogs.com/simly/p/10863327.html
Copyright © 2011-2022 走看看