消费者

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * 消费者类
 * */
public class Consumer implements Runnable {
	private BlockingDeque<String> queue = new LinkedBlockingDeque<String>();

	public Consumer(BlockingDeque<String> queue){
		this.queue = queue;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		int i = 0;
			do{
				try {
					String product = queue.take();
					if(!"".equals(product) && null != product){
						System.out.println("消费者-:"+product);
						i++;
					}
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}while(i < 50);
		}
}

生产者

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
/**
 * 生产者类
 * */

public class Producer implements Runnable {
	private BlockingDeque<String> queue = new LinkedBlockingDeque<String>();

	public Producer(BlockingDeque<String> queue){
		this.queue = queue;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int i=0; i < 50; i++){
			try {
				System.out.println("生产者-Product"+i);
				queue.put("Prodect:"+i);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

测试类

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
javaimport java.util.concurrent.BlockingDeque;
       import java.util.concurrent.LinkedBlockingDeque;

       /**
        * 测试类
        * */
       public class BlockingQueueTest {

       	private static BlockingDeque<String> queue = new LinkedBlockingDeque<String>();

       	public static void main(String[] args) {
       		// TODO Auto-generated method stub
       		Producer producer = new Producer(queue);
       		Consumer cusumer = new Consumer(queue);
       		new Thread(cusumer).start();
       		new Thread(producer).start();
       	}
       }