生産者コンシューマチーム

1429 ワード

ブロックキューで実装
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

public class Test927 extends Thread {
	public void run() {
		Customer a = new Customer();
		a.start();
		Producer b = new Producer();
		b.start();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Test421 c = new Test421();
		c.start();
	}

	int key = 0;
	BlockingDeque<Integer> deque=new LinkedBlockingDeque<Integer>(1000);

	class Customer extends Thread {
		public void run() {
			while (true) {

				try {
					int key=deque.take();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println(" " + deque.size());			


				
			}
		}
	}

	class Producer extends Thread {
		public void run() {
			while (true) {
					try {
						deque.put(1);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					System.out.println(" " + deque.size());

					

				
			}
		}
	}
}