1、wait()和notify()方法的实现
2、可重入锁ReentrantLock的实现
3、阻塞队列BlockingQueue的实现
4、信号量Semaphore的实现
5、管道输入输出流PipedInputStream和PipedOutputStream实现
1、wait()和notify()方法的实现
public class ProducerConsumerWithWaitNofity {
private static Integer count = 0;
private static final Integer FULL = 10;
private static String LOCK = "lock";
class Producer implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
synchronized (LOCK) {
while (count == FULL) {
try {
LOCK.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count++;
LOCK.notifyAll();
}
}
}
}
class Consumer implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
synchronized (LOCK) {
while (count == 0) {
try {
LOCK.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count--;
LOCK.notifyAll();
}
}
}
}
}
2、可重入锁ReentrantLock的实现
public class ProducerConsumerWithReentrantLock {
private static Integer count = 0;
private static final Integer FULL = 10;
private Lock lock = new ReentrantLock();
//创建两个条件变量,一个为缓冲区非满,一个为缓冲区非空
private final Condition notFull = lock.newCondition();
private final Condition notEmpty = lock.newCondition();
class Producer implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
lock.lock();
try {
while (count == FULL) {
try {
notFull.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count++;
notEmpty.signal();
} finally {
lock.unlock();
}
}
}
}
class Consumer implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
lock.lock();
try {
while (count == 0) {
try {
notEmpty.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count--;
notFull.signal();
} finally {
lock.unlock();
}
}
}
}
}
3、阻塞队列BlockingQueue的实现
public class ProducerConsumerWithBlockingQueue {
private static Integer count =0;
final BlockingQueue blockingQueue = new ArrayBlockingQueue(10);
class Producer implements Runnable{
@Override
public void run() {
for (int i=0;i<10;i++){
try {
blockingQueue.put(1);
count++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable{
@Override
public void run() {
for (int i=0;i<10;i++){
try {
blockingQueue.take();
count--;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
4、信号量Semaphore的实现
public class ProducerConsumerWithSemaphore {
private static Integer count = 0;
//创建三个信号量
final Semaphore notFull = new Semaphore(10);
final Semaphore notEmpty = new Semaphore(0);
//mutex信号量,维护生产者消费者之间的同步关系,保证生产者和消费者之间的交替进行
final Semaphore mutex = new Semaphore(1);
class Producer implements Runnable {
@Override
public void run() {
for (int i=0;i<10;i++){
try{
notFull.acquire();
mutex.acquire();
count++;
}catch (InterruptedException e){
e.printStackTrace();
}finally {
mutex.release();
notEmpty.release();
}
}
}
}
class Consumer implements Runnable {
@Override
public void run() {
for (int i=0;i<10;i++){
try{
notEmpty.acquire();
mutex.acquire();
count--;
}catch (InterruptedException e){
e.printStackTrace();
}finally {
mutex.release();
notFull.release();
}
}
}
}
}
5、管道输入输出流PipedInputStream和PipedOutputStream实现
public class ProducerConsumerWithIO {
//用生产者线程往管道输出流中写入数据,消费者在管道输入流中读取数据
final PipedInputStream pis = new PipedInputStream();
final PipedOutputStream pos = new PipedOutputStream();
{
try {
pis.connect(pos);
} catch (IOException e) {
e.printStackTrace();
}
}
class Producer implements Runnable {
@Override
public void run() {
try {
while (true) {
int num = (int) (Math.random() * 255);
pos.write(num);
pos.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
pos.close();
pis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable {
@Override
public void run() {
try {
while (true) {
int num = (int) (Math.random() * 255);
int read = pis.read();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
pos.close();
pis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}