java线程同步的实现_【Java多线程系列三】实现线程同步的方法

packagecom.concurrent.test;importjava.util.Stack;importjava.util.concurrent.ArrayBlockingQueue;importjava.util.concurrent.BlockingQueue;importjava.util.concurrent.locks.Condition;importjava.util.concurrent.locks.ReentrantLock;/*** @Description: 三种方法实现生产者/消费者*/

public classThreadSynchronizeTest {public static voidmain(String[] args) {

ProducerConsumer producerConsumer= newProducerConsumerViaBlockingQueue();

producerConsumer.test();

}

}abstract classProducerConsumer {protected int capacity = 10;protected int element = 0;protected abstract void produce() throwsInterruptedException;protected abstract void consume() throwsInterruptedException;public voidtest() {

Thread producer= new Thread(newRunnable() {

@Overridepublic voidrun() {while (true) {try{

produce();

}catch(InterruptedException e) {

e.printStackTrace();

}

}

}

});

Thread consumer= new Thread(newRunnable() {

@Overridepublic voidrun() {while (true) {try{

consume();

}catch(InterruptedException e) {

e.printStackTrace();

}

}

}

});

producer.start();

consumer.start();

}

}/*** 方法一:ReentrantLock结合Condition*/

class ProducerConsumerViaReentrantLock extendsProducerConsumer {private Stack stack = new Stack<>();private ReentrantLock lock = newReentrantLock();private Condition notFull =lock.newCondition();private Condition notEmpty =lock.newCondition();

@Overrideprotected void produce() throwsInterruptedException {try{

lock.lock();if (stack.size() ==capacity) {

notFull.await();

}++element;

System.out.println(Thread.currentThread().getId()+ "," + Thread.currentThread().getName() + " produce " +element);

stack.push(element);

notEmpty.signalAll();

Thread.sleep(1000L);

}finally{

lock.unlock();

}

}

@Overrideprotected void consume() throwsInterruptedException {try{

lock.lock();if(stack.isEmpty()) {

notEmpty.await();

}int element =stack.pop();

System.out.println(Thread.currentThread().getId()+ "," + Thread.currentThread().getName() + " consume " +element);

notFull.signalAll();

}finally{

lock.unlock();

}

}

}/*** 方法二:synchronized 结合 wait、notify、notifyAll*/

class ProducerConsumerViaObjectLock extendsProducerConsumer {private Stack stack = new Stack<>();private Object lock = newObject();

@Overrideprotected void produce() throwsInterruptedException {/** 1,lock为监视器

* 2,wait/notify/notifyAll方法必须在synchronized块内调用

* 3,调用wait/notify/notifyAll方法但不持有监视器的使用权将会抛出java.lang.IllegalMonitorStateException*/

synchronized(lock) {if (stack.size() ==capacity) {

lock.wait();

}++element;

System.out.println(Thread.currentThread().getId()+ "," + Thread.currentThread().getName() + " produce " +element);

stack.push(element);

lock.notifyAll();

Thread.sleep(1000L);

}

}

@Overrideprotected void consume() throwsInterruptedException {synchronized(lock) {if(stack.isEmpty()) {

lock.wait();

}int element =stack.pop();

System.out.println(Thread.currentThread().getId()+ "," + Thread.currentThread().getName() + " consume " +element);

lock.notifyAll();

}

}

}/*** 方法三:BlockingQueue*/

class ProducerConsumerViaBlockingQueue extendsProducerConsumer {private BlockingQueue queue = new ArrayBlockingQueue<>(capacity);

@Overrideprotected void produce() throwsInterruptedException {++element;

System.out.println(Thread.currentThread().getId()+ "," + Thread.currentThread().getName() + " produce " +element);

queue.put(element);

Thread.sleep(1000L);

}

@Overrideprotected void consume() throwsInterruptedException {int element =queue.take();

System.out.println(Thread.currentThread().getId()+ " consume " +element);

Thread.sleep(10000L);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值