only code
tradation Demo
package JavaAdvanced;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author 韦海涛
* @version 1.0
* @date 4/7/2021 9:40 AM
*/
class ShareData{
private int number = 0;
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public void increment(){
lock.lock();
try {
//生产者
//如果当前库存不为0时。也就是当前仓库还有存货 则不需要进行生产
while ( number>=10){
condition.await();
}
number++;
System.out.println(Thread.currentThread().getName()+"\t"+number);
//通知唤醒
condition.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
public void decrement(){
lock.lock();
try {
//消费者
//如果当前资源为0则等待
while (number==0){
condition.await();
}
number--;
System.out.println(Thread.currentThread().getName()+"\t"+number);
//通知唤醒
condition.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
public class ProConsumer_TraditionDemo {
public static void main(String[] args) {
ShareData shareData = new ShareData();
//生产
new Thread(()->{
for (int i = 0; i < 100; i++) {
shareData.increment();
}
},"pro").start();
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int j = 0; j < 10; j++) {
//消费1
new Thread(()->{
for (int i = 0; i < 10; i++) {
shareData.decrement();
}
},"cust"+j).start();
}
}
}
useSynchronousQueueDemo:
package JavaAdvanced;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;
/**
* @author 韦海涛
* @version 1.0
* @date 4/7/2021 8:48 AM
*
* ---对用生产者-消费者模式---
* 当阻塞队列是空的时候,从队列中获取元素的操作将会被阻塞--商品被卖光了
* 当阻塞队列是满的时候,往队列里添加元素的操作将会被阻塞--仓库已满
*
* 1.阻塞队列
* 1.1阻塞队列有咩有好的一面
* 在某些情况会挂起线程,一旦条件满足,被挂起的线程又会被自动唤醒
* 我们不需要关系什么时候需要阻塞线程,什么时候需要唤醒线程
* 1.2不得不阻塞.该如何管理
* 使用blockingQueue
*/
public class BolckingQueueDemo {
public static void main(String[] args) {
SynchronousQueue<String> synchronousQueue = new SynchronousQueue<>();
new Thread(()->{
try {
System.out.println(Thread.currentThread().getName()+"\t put 1");
synchronousQueue.put("1");
System.out.println(Thread.currentThread().getName()+"\t put 2");
synchronousQueue.put("2");
System.out.println(Thread.currentThread().getName()+"\t put 3");
synchronousQueue.put("3");
} catch (InterruptedException e) {
e.printStackTrace();
}
},"AAAA").start();
new Thread(()->{
try {
TimeUnit.SECONDS.sleep(3);
System.out.println(Thread.currentThread().getName()+"\t take 1");
System.out.println(Thread.currentThread().getName()+"\t"+synchronousQueue.take());
System.out.println(Thread.currentThread().getName()+"\t take 2");
System.out.println(Thread.currentThread().getName()+"\t"+synchronousQueue.take());
System.out.println(Thread.currentThread().getName()+"\t take 3");
System.out.println(Thread.currentThread().getName()+"\t"+synchronousQueue.take());
synchronousQueue.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"BBB").start();
}
}