生产者/消费者问题的实现(JAVA)

1.阻塞队列
class producer implements Runnable{
    private final BlockingQueue sharedQueue;
    public producer(BlockingQueue sharedQueue){
        this.sharedQueue=sharedQueue;
    }
    public void run(){
    	while(true){
       for(int i=0;i<10;i++){
            try{
                System.out.println("producer products:"+i);
                sharedQueue.put(i);
            }catch(Exception e){
                System.out.println(e);
            }
       }
    }
    }
}
class consumer implements Runnable{
    private final BlockingQueue sharedQueue;
    public consumer(BlockingQueue sharedQueue){
        this.sharedQueue=sharedQueue;
    }
    public void run(){
    	while(true){
            try{
                int i=(Integer) sharedQueue.take();
                System.out.println("consumd: "+i);
            }catch(Exception e){
                System.out.println(e);
            }
    	}
        
    }
}
public class Main {

    public static void main (String[] args) {
        BlockingQueue sharedQueue=new LinkedBlockingQueue(10);
        
        Thread proThread=new Thread(new producer(sharedQueue));
        Thread consThread=new Thread(new consumer(sharedQueue));
        
        proThread.start();
        consThread.start();
       
    }

}

2.wait/notify()方法

class produce implements Runnable{
    public Queue sharedQueue;
    public produce(Queue sharedQueue){
        this.sharedQueue=sharedQueue;
    }
    public void run(){
    	while(true){
        synchronized(sharedQueue){
            while(sharedQueue.size()==10){
                try{
                    sharedQueue.wait();
                }catch(Exception e){
                    System.out.println(e);
                    sharedQueue.notifyAll();
                }
            }
            sharedQueue.offer(1);
            System.out.println("producting");
            sharedQueue.notifyAll();
        }
    }
    }
}

class consumer implements Runnable{
    public Queue sharedQueue;
    public consumer(Queue sharedQueue){
        this.sharedQueue=sharedQueue;
    }
    public void run(){
    	while(true){
        synchronized(sharedQueue){
            while(sharedQueue.size()==0){
                try{
                    sharedQueue.wait();
                }catch(Exception e){
                    System.out.println(e);
                    sharedQueue.notifyAll();
                    }
        }
        sharedQueue.poll();
        System.out.println("consuming");
        sharedQueue.notifyAll();
        
    }
    	}
}
}
public class Main {
    public static PriorityQueue<Integer> pq;
    

    public static void main (String[] args) {
    	
    
    	pq=new PriorityQueue<Integer>(10);
    
        Thread pT=new Thread(new produce(pq));
        Thread cT=new Thread(new consumer(pq));
       pT.start();
       cT.start();
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值