JAvaはLinkedBlockingQueueを用いて生産者消費者モデルを実現する


JavaでLinkedBlockingQueueを使用して生産者、消費者モデルを実現
LinkedBlockingQueue実装はスレッドが安全で、FIFO(先進先出)などの特性を実現している.生産者消費者の第一選択として、LinkedBlockingQueueは容量を指定してもよいし、指定しなくてもよいし、指定しなければデフォルト最大はInteger.MAX_VALUEでは、主にputメソッドとtakeメソッドが使用され、putメソッドはキューがいっぱいになるとキューメンバーが消費されるまでブロックされ、takeメソッドはキューが空いているときにブロックされ、キューメンバーが入るまでブロックされます.
本の話は二度と繰り返さないで、やはりインスタンスコードを見てみましょう.
工場生産製造  生産は高くて、美人もいます.
消費者にはX 2世代も監督もいる.
消費者に資源を奪わせよう.
生産者実現
package cn.hpc.producerConsumer;

import java.util.UUID;
import java.util.concurrent.BlockingQueue;

public class Producer implements Runnable {
    private BlockingQueue queue;
    private String produce;
    public Producer(BlockingQueue queue, String produce) {
        this.queue = queue;
        if (null != produce)
        	this.produce = produce;
        else this.produce = "null ";
    }

    @Override
    public void run() {
        String uuid = UUID.randomUUID().toString();
        try {
        	Thread.sleep(200);//      
            queue.put(produce + " : " + uuid);
            System.out.println("Produce \"" + produce + "\" : " + uuid + " " + Thread.currentThread());
            
        } catch (InterruptedException e) {
            System.out.println(e.getMessage());
        }
    }
}       

消費者
package cn.hpc.producerConsumer;

import java.util.concurrent.BlockingQueue;

public class Consumer implements Runnable {
	private BlockingQueue queue;
	private String consumer;

	public Consumer(BlockingQueue queue, String consumer) {
		this.queue = queue;
		if (null != consumer)
			this.consumer = consumer;
		else
			this.consumer = "null ";
	}

	@Override
	public void run() {
		try {
			String uuid = queue.take();
			System.out.println(consumer + " decayed " + uuid
					+ " " + Thread.currentThread());
		} catch (InterruptedException e) {
			System.out.println(e.getMessage());
		}
	}
}

呼び出し:  new Tester();
やる
package cn.hpc.producerConsumer;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

public class Tester {

    public Tester(){
        //   
        LinkedBlockingQueue queue = new LinkedBlockingQueue(10);

        
        ExecutorService service = Executors.newCachedThreadPool();
        for (int i = 0; i < 6; i++) {
        	service.submit(new Consumer(queue, "X  " + i));
        	service.submit(new Consumer(queue, "  " + i));
        }
        for (int i = 0; i < 6; i++) {
        	service.submit(new Producer(queue, "   ," + i));
        	service.submit(new Producer(queue, "    " + i));
        }
        service.shutdown();
    }
}    

出力ログを見て
12-26 12:13:07.689: I/System.out(19372): Produce "   0" : 67cbb3c8-b72b-4bd5-9edc-f1c3314e9f50 Thread[pool-1-thread-13,5,main]
12-26 12:13:07.699: I/System.out(19372):   4 decayed     4 : 73e6939e-287e-4dda-88ff-2a59871f8a41 Thread[pool-1-thread-10,5,main]
12-26 12:13:07.699: I/System.out(19372): Produce "   1" : 21f150e3-7909-47c3-a5b1-31b4f4242446 Thread[pool-1-thread-15,5,main]
12-26 12:13:07.699: I/System.out(19372): X  5 decayed    5 : 66d5449b-ad38-41fe-8012-224b0f996697 Thread[pool-1-thread-11,5,main]
12-26 12:13:07.699: I/System.out(19372): Produce "   5" : 66d5449b-ad38-41fe-8012-224b0f996697 Thread[pool-1-thread-23,5,main]
12-26 12:13:07.699: I/System.out(19372):   5 decayed     5 : d6008dee-c42f-4c09-8856-ae38ac64e104 Thread[pool-1-thread-12,5,main]
12-26 12:13:07.699: I/System.out(19372): Produce "    1" : 9786647d-c499-40be-9905-da767ae2fe88 Thread[pool-1-thread-16,5,main]
12-26 12:13:07.699: I/System.out(19372): Produce "    3" : 72fcbcec-c903-4310-886a-40e31c693248 Thread[pool-1-thread-20,5,main]
12-26 12:13:07.699: I/System.out(19372): X  2 decayed    2 : 75f1952c-975d-41b1-ba69-9e24006031cd Thread[pool-1-thread-5,5,main]
12-26 12:13:07.699: I/System.out(19372):   2 decayed     2 : adb8b376-83c4-487b-9af1-11c16d060ee4 Thread[pool-1-thread-6,5,main]
12-26 12:13:07.699: I/System.out(19372):   3 decayed     3 : 72fcbcec-c903-4310-886a-40e31c693248 Thread[pool-1-thread-8,5,main]
12-26 12:13:07.699: I/System.out(19372): Produce "   2" : 75f1952c-975d-41b1-ba69-9e24006031cd Thread[pool-1-thread-17,5,main]
12-26 12:13:07.699: I/System.out(19372): Produce "    0" : aad2007e-6322-43bc-a1f3-ade9af671e7b Thread[pool-1-thread-14,5,main]
12-26 12:13:07.699: I/System.out(19372): X  0 decayed    0 : 67cbb3c8-b72b-4bd5-9edc-f1c3314e9f50 Thread[pool-1-thread-1,5,main]
12-26 12:13:07.699: I/System.out(19372):   0 decayed     0 : aad2007e-6322-43bc-a1f3-ade9af671e7b Thread[pool-1-thread-2,5,main]
12-26 12:13:07.699: I/System.out(19372): X  1 decayed    1 : 21f150e3-7909-47c3-a5b1-31b4f4242446 Thread[pool-1-thread-3,5,main]
12-26 12:13:07.699: I/System.out(19372):   1 decayed     1 : 9786647d-c499-40be-9905-da767ae2fe88 Thread[pool-1-thread-4,5,main]
12-26 12:13:07.699: I/System.out(19372): Produce "   3" : c0ff371e-473d-4af0-90a7-620fa67c22d8 Thread[pool-1-thread-19,5,main]
12-26 12:13:07.699: I/System.out(19372): Produce "    5" : d6008dee-c42f-4c09-8856-ae38ac64e104 Thread[pool-1-thread-24,5,main]
12-26 12:13:07.699: I/System.out(19372): Produce "   4" : 272cb2a6-b1dd-44d8-8254-df4f6be59bd2 Thread[pool-1-thread-21,5,main]
12-26 12:13:07.699: I/System.out(19372): Produce "    2" : adb8b376-83c4-487b-9af1-11c16d060ee4 Thread[pool-1-thread-18,5,main]
12-26 12:13:07.699: I/System.out(19372): Produce "    4" : 73e6939e-287e-4dda-88ff-2a59871f8a41 Thread[pool-1-thread-22,5,main]
12-26 12:13:07.699: I/System.out(19372): X  3 decayed    3 : c0ff371e-473d-4af0-90a7-620fa67c22d8 Thread[pool-1-thread-7,5,main]
12-26 12:13:07.699: I/System.out(19372): X  4 decayed    4 : 272cb2a6-b1dd-44d8-8254-df4f6be59bd2 Thread[pool-1-thread-9,5,main]