生產者消費者實體
1.ReentrantLock實作
public class Demo {
public static void main(String[] args) {
Data data=new Data();
//兩個生產者執行緒
for (int i = 0; i < 2; i++) {
new Thread(()->{
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"生產者"+String.valueOf(i)).start();
}
//兩個消費者執行緒
for (int i = 2; i < 4; i++) {
new Thread(()->{
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"消費者"+String.valueOf(i)).start();
}
}
}
class Data{
//共享資源
private int number=0;
Lock lock=new ReentrantLock();
Condition condition= lock.newCondition();
public void increment() throws InterruptedException {
//上鎖
lock.lock();
try {
//回圈判斷條件,不用if避免虛假喚醒
while (number!=0){
//不滿足條件就等待
condition.await();
}
//操作 生產
number++;
System.out.println(Thread.currentThread().getName()+"執行緒\t"+number);
//通知喚醒等待的消費者執行緒
condition.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
//解鎖
lock.unlock();
}
public void decrement() throws InterruptedException {
//上鎖
lock.lock();
try {
while (number==0){
condition.await();
}
number--;
System.out.println(Thread.currentThread().getName()+"執行緒\t"+number);
condition.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
lock.unlock();
}
}
2.阻塞佇列實作
public class 阻塞佇列版 {
public static void main(String[] args) {
MyResource myResource=new MyResource(new ArrayBlockingQueue(10));
new Thread(()->{
System.out.println("生產執行緒啟動");
try {
myResource.myProd();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"Prod").start();
new Thread(()->{
System.out.println("消費執行緒啟動");
try {
myResource.myConsumer();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"Consumer").start();
try {
Thread.sleep(5000);
System.out.println();
System.out.println();
System.out.println();
System.out.println();
myResource.stop();
System.out.println("5秒時間到,老板main執行緒叫停");
} catch (InterruptedException e) {
}
}
}
class MyResource{
//默認開啟,開始生產消費
private volatile boolean FLAG=true;
private AtomicInteger atomicInteger=new AtomicInteger();
BlockingQueue<String> blockingQueue=null;
public MyResource(BlockingQueue blockingQueue1){
blockingQueue=blockingQueue1;
System.out.println(blockingQueue1.getClass().getName());
}
//生產方法
public void myProd() throws InterruptedException {
String data=null;
boolean result;
while (FLAG){
data=atomicInteger.incrementAndGet()+"";
result=blockingQueue.offer(data,2L, TimeUnit.SECONDS);
if(result){
System.out.println(Thread.currentThread().getName()+"\t 插入佇列"+data+"成功");
}else{
System.out.println(Thread.currentThread().getName()+"\t 插入佇列"+data+"失敗");
}
TimeUnit.SECONDS.sleep(1);
}
System.out.println("生產結束");
}
//消費方法
public void myConsumer() throws InterruptedException {
String rs=null;
while (FLAG){
rs = blockingQueue.poll(2L, TimeUnit.SECONDS);
if (rs==null||rs.equalsIgnoreCase(""))
{
FLAG=false;
System.out.println(Thread.currentThread().getName()+"超過兩秒鐘沒取到商品,退出");
System.out.println();
System.out.println();
System.out.println();
return; //
}
System.out.println(Thread.currentThread().getName()+"\t 消費佇列"+rs+"成功");
}
}
public void stop(){
this.FLAG=false;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/423180.html
標籤:其他
上一篇:資料分析 -- Pandas③
