`

线程同步:生产者和消费者问题

阅读更多

1、实现一个生产者

       

public class Producer implements Runnable {

	SyncStack stack;	//数据存放位置,和生产者是相同的位置

	public Producer(SyncStack s){
		stack = s;
	}
	public void run(){
		for(int i=0; i<20; i++){
			char c =(char)(Math.random()*26+'A');
			stack.push(c);//产品生产后,存放产品
			System.out.println("produced:"+c);
			try{							        
				Thread.sleep((int)(Math.random()*1000)); 
			}catch(InterruptedException e){
			}
		}
	}
}

 

2、实现消费者

   

public class Consumer implements Runnable {
	SyncStack stack;	//数据存放位置,和生产者是相同的位置
	public Consumer(SyncStack s){
		stack = s;
	}
	public void run(){
		for(int i=0;i<60;i++){
			char c = stack.pop();//往外拿产品,进行产品消费
			System.out.println("消费:"+c);
			try{							           
				Thread.sleep((int)(Math.random()*1000));
			}catch(InterruptedException e){
			}
		}
	}

}

 3、实现存放位置的线程同步

 

public class SyncStack {
	private int index = 0;
	private char []data = new char[6];	
	public synchronized void push(char c){
		while(index == data.length){
		try{
				this.wait();
				//某线程执行到这里时,篮子已经满了,那么就等待消费者消费。此时锁已经不再属于该线程
				
		}catch(InterruptedException e){}
		}
		this.notifyAll();//唤醒其他线程(消费者线程或者其他生产者线程)
		                     //主要是为了让消费者消费,才能唤醒其他等待的生产者线程
		                 
		data[index] = c;
		index++;
	}
	public synchronized char pop(){
		while(index ==0){
			try{
				this.wait();
				//某线程执行到这里时,篮子已经空了,那么就等待生产者生产,此时锁已经不属于该线程
				
			}catch(InterruptedException e){}
		}
		this.notifyAll();//唤醒其他线程(生产者线程或者其他消费这线程)
		                 //主要是为了让生产者生产,才能唤醒其它等待的消费者线程
		index--;           
		return data[index];
	}
}

 

5、测试类

    

public class ProducerConsumer {
	 public static void main(String args[]){
		  SyncStack stack = new SyncStack();//共享存储空间
		 Producer p=new Producer(stack);
		  Consumer c = new Consumer(stack);
	                  new Thread(p).start();//生产者线程
		 new Thread(p).start();//生产者线程
		 new Thread(p).start();//生产者线程
		 new Thread(c).start();//消费者线程
 }
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics