您的位置:首页 > 编程语言 > Java开发

java线程“生产/消费”模型2

2013-06-01 00:19 309 查看
/*  资源类 */
class ShareValue {
private int total;
//判断对象是否为空
private boolean isEmpty=true;
//判断对象是否已满
private boolean isFull=true;

public ShareValue(int total) {
this.total = total;
if(total>0) isEmpty=false;
if(total<1000) isFull=false;
}
/*
* synchronized 方法控制对类成员变量的访问:每个类实例对应一把锁,每个 synchronized 方法都必须获得调用该方法的类实例的锁方能执行,
* 否则所属线程阻塞,方法一旦执行,就独占该锁,直到从该方法返回时才将锁释放,此后被阻塞的线程方能获得该锁,重新进入可执行状态。
* 这种机制确保了同一时刻对于每一个类实例,其所有声明为 synchronized 的成员函数中至多只有一个处于可执行状态
* (因为至多只有一个能够获得该类实例对应的锁),从而有效避免了类成员变量的访问冲突(只要所有可能访问类成员变量的方法均被声明为 synchronized)。
* */
synchronized void putValue(int value){
//取得当前线程名
String name=Thread.currentThread().getName();
while(isFull){
display("Full ! ["+name+"] waites.");
try {
this.wait();
} catch (InterruptedException e) {
System.out.println(e);
}
}
total+=value;
if(total>1000){
isFull=true;
}
display(name+",put: "+value+",old value: "+(total-value)+",now value"+total);
isEmpty=false;
//通知所有的等待线程
notifyAll();
}

synchronized int getValue(int value){
String  name=Thread.currentThread().getName();
while(isEmpty || total<value){
display("Empty or not enough! ["+name+"] waits.");
try {
//进入等待状态
this.wait();
} catch (InterruptedException e) {
System.out.println(e);
}
}
display(name+" get: "+value+",old value:"+total+",now value :"+(total-value));
if(total-value>=0){
total-=value;
}
if(total==0){
isEmpty=true;
}
if(total<1000){
isFull=false;
}
//通知所有等待的线程
notifyAll();
return value;
}

int getNowTotal(){
return total;
}

private void display(String string) {
System.out.println(string);
}
}

/*  生产者类  */
class Producer extends Thread {
// 共享的ShareValue对象
ShareValue share;
// 要增加的值
int value;

public Producer(String name, ShareValue share, int value) {
super(name);
this.share = share;
this.value = value;
}

public void run() {
//同步share对象 ,直到当前代码块运行完毕后,share的对象锁才会释放
synchronized (share) {
try {
sleep(100);
} catch (InterruptedException e) {
System.out.println(e);
}
share.putValue(value);

}

}
}

/*消费者类*/
class Consumer extends Thread{
//共享的ShareValue对象
ShareValue share;
//要减少的值
int value;
public Consumer(String name,ShareValue share, int value) {
super(name);
this.share = share;
this.value = value;
}
public void run(){
//同步share对象,直到当前代码运行完毕后,share的对象锁才会释放
synchronized (share) {
try {
sleep(100);
} catch (InterruptedException e) {
System.out.println(e);
}
share.getValue(value);
}

}
}

/*  测试主类  */
public class TestDemo {

public static void main(String[] args) {
ShareValue share=new ShareValue(0);
Producer producer1=new Producer("producer1", share, 100);
Consumer consumer=new Consumer("consumer", share, 300);
Producer producer2=new Producer("producer2",share,950);
Producer producer3=new Producer("producer3",share,50);
//		Producer producer4=new Producer("producer4",share,50);
//		Producer producer5=new Producer("producer5",share,50);
producer1.start();
consumer.start();
producer2.start();
producer3.start();
//		producer4.start();
//		producer5.start();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: