java的wait和notify

来源:互联网 发布:程序员简历 知乎 编辑:程序博客网 时间:2024/05/02 17:55
package com.jimmy.lock;


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


public class TestWaitNotify {


public static void main(String[] args) {
// TODO Auto-generated method stub
Product  a=new Product();
// Produce p=new Produce(a);
// p.setNum(4);
//
// Comsume c=new Comsume(a);
// c.setNum(5);

//Produce p=null;




//Comsume c=null;


for(int i=1;i<=10;i++)
{
Produce p=new Produce(a);
p.setNum(((int)(Math.random()*10)));

Comsume c=new Comsume(a);
c.setNum(((int)(Math.random()*10)));
Thread t=new Thread(p);
Thread t2=new Thread(c);
t2.start();
t.start();
}


}


}


//同步方法全部包装到Product 里面

class Product {
// public static final int MAX_SIZE = 100;
//
// private LinkedList<Object> a = new LinkedList<Object>();
// Product()
// {
//
// }
//
// public List getA() {
// return a;
// }
//
// public void setA(LinkedList a) {
// this.a = a;
// }
public  void  produce(int num)
{

synchronized(list){
while(num+list.size()>MAX_SIZE)
{
System.out.println(Thread.currentThread().getName()+":要生产的产品数量:" + num + "库存量"  
                    + list.size() + "暂不能执行生产任务");  

try {
list.wait();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for (int i = 0; i < num; i++) {  
             list.add(new Object());  
         } 
System.out.println(Thread.currentThread().getName()+":已经生产产品数:" + num + "现库存量为:" + list.size());  
list.notifyAll();
}
}

public  void  consume(int num)
{
synchronized(list)
{
while(list.size()<num)
{
System.out.println(Thread.currentThread().getName()+":要消费的产品数量:" + num + "库存量:"  
                    + list.size() + "暂时不能执行消费任务!"); 

try {
list.wait();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for (int i = 0; i <num; ++i) {  
list.remove();  
        }  
System.out.println(Thread.currentThread().getName()+":已经消费的产品数:" + num + "现库存量为:" + list.size());  
list.notifyAll();
}
}
//
//
    // 仓库最大存储量
    private final int MAX_SIZE = 100;


    // 仓库存储的载体
    private LinkedList<Object> list = new LinkedList<Object>();


    


    // get/set方法
    public LinkedList<Object> getList()
    {
        return list;
    }


    public void setList(LinkedList<Object> list)
    {
        this.list = list;
    }


    public int getMAX_SIZE()
    {
        return MAX_SIZE;
    }
}


class Produce implements Runnable{
private  Product a;
private int num;  
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
Produce(Product aa)
{a=aa;}
public  void  produce()
{
a.produce(num);
//System.out.println("produce a="+a.getA().size());
}

@Override
public void run() {
this.produce();

}
}
class Comsume implements Runnable{
private Product a;
private int num;
public int getNum() {
return num;
}


public void setNum(int num) {
this.num = num;
}


Comsume(Product aa)
{a=aa;}

public  void  comsume()
{
a.consume(num);
// System.out.println("comsume a="+a.getA().size());
}
@Override
public void run() {
this.comsume();

}
}
0 0
原创粉丝点击