java 消费者 线程池_线程池 生产者 消费者 模式 JAVA进阶

package javajinjie.char29.threadpool;

import java.util.concurrent.*;

import java.util.concurrent.locks.*;

public class ConsumerProducer {

private static Buffer buffer = new Buffer();

public static void main(String[] args) {

// Create a thread pool with two threads

ExecutorService executor = Executors.newFixedThreadPool(2);

executor.execute(new ProducerTask());

executor.execute(new ConsumerTask());

executor.shutdown();

}

// A task for adding an int to the buffer

private static class ProducerTask implements Runnable {

public void run() {

try {

int i = 1;

while (true) {

System.out.println("Producer writes " + i);

buffer.write(i++); // Add a value to the buffer

// Put the thread into sleep

Thread.sleep((int)(Math.random() * 10000));

}

} catch (InterruptedException ex) {

ex.printStackTrace();

}

}

}

// A task for reading and deleting an int from the buffer

private static class ConsumerTask implements Runnable {

public void run() {

try {

while (true) {

System.out.println("\t\t\tConsumer reads " + buffer.read());

// Put the thread into sleep

Thread.sleep((int)(Math.random() * 10000));

}

} catch (InterruptedException ex) {

ex.printStackTrace();

}

}

}

// An inner class for buffer

private static class Buffer {

private static final int CAPACITY = 1; // buffer size

private java.util.LinkedList queue =

new java.util.LinkedList();

// Create a new lock

private static Lock lock = new ReentrantLock();

// Create two conditions

private static Condition notEmpty = lock.newCondition();

private static Condition notFull = lock.newCondition();

public void write(int value) {

lock.lock(); // Acquire the lock

try {

while (queue.size() == CAPACITY) {

System.out.println("Wait for notFull condition");

notFull.await();

}

queue.offer(value);

System.out.println("write "+ value + " done");

notEmpty.signal(); // Signal notEmpty condition

} catch (InterruptedException ex) {

ex.printStackTrace();

} finally {

lock.unlock(); // Release the lock

}

}

public int read() {

int value = 0;

lock.lock(); // Acquire the lock

try {

while (queue.isEmpty()) {

System.out.println("\t\t\tWait for notEmpty condition");

notEmpty.await();

}

value = queue.remove();

System.out.println("read "+ value + " done");

notFull.signal(); // Signal notFull condition

} catch (InterruptedException ex) {

ex.printStackTrace();

} finally {

lock.unlock(); // Release the lock

return value;

}

}

}

}

运行结果

Producer writes 1 write 1 done read 1 doneConsumer reads 1Wait for notEmpty condition Producer writes 2 write 2 done read 2 doneConsumer reads 2Wait for notEmpty condition Producer writes 3 write 3 done read 3 doneConsumer reads 3 Producer writes 4 write 4 done read 4 doneConsumer reads 4 Producer writes 5 write 5 done read 5 doneConsumer reads 5Wait for notEmpty condition Producer writes 6 write 6 done read 6 doneConsumer reads 6Wait for notEmpty condition Producer writes 7 write 7 done read 7 doneConsumer reads 7Wait for notEmpty condition

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值