1,使用阻塞隊列blockingqueue實現簡單的生產者消費者模型
原理:阻塞隊列BlockingQueue本身就是線程安全,同時使用阻塞隊列提供的take,put方法在操作阻塞隊列會是使得隊列進入阻塞。因此阻塞隊列就是線程安全的。基於阻塞隊列以上的性質就可以實現線程安全的生產者消費者模型。
代碼如下:
package 比較器java特性;
// 此代碼中使用了Lambda表達式來作爲參數完成進程的我創建
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
public class BlockingQueuePC {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<String> blockingqueue=new LinkedBlockingQueue<>(2);
CountDownLatch cd = new CountDownLatch(2);
Thread t1 = new Thread(()->{
try {
for(int i=0;i<10;i++)
{
blockingqueue.put("goods"+i);
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
cd.countDown();
}
});
Thread t2 = new Thread(()->{
try {
for(int i=0;i<10;i++)
{
System.out.println(blockingqueue.take());
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
cd.countDown();
}
});
t2.start();
t1.start();
cd.await();
System.out.println(blockingqueue.size());
}
}
2, 使用信號量Semaphore來實現生產者消費者同步訪問資源問題
原理:根據操作系統的信號量來對臨界資源進行同步訪問,本問題主要有三個臨界資源分別就是full(當前的容量或者說產品數量),empty(表示當前的容器的空閒容量),mutex(這個就比較簡單表示讀寫鎖,並且一個時刻只允許一個線程進行訪問)。只要控制 好對這三個臨界資源的訪問就可以實現生產者消費者模式模型。
代碼如下:
package 比較器java特性;
import java.util.*;
import java.util.concurrent.Semaphore;
public class SemaphorePC {
// N是容量
private final static int N=10;
// full是容量,empty是空閒容量,mutex是讀寫鎖
private static Semaphore full,empty,mutex;
// 記錄當前產品的數量
private static volatile int count=0;
static {
// 開始容量初始化有0個產品
full = new Semaphore(0);
// 開始剩餘容量爲N
empty = new Semaphore(N);
// 設置只有一個線程獲取讀寫鎖
mutex = new Semaphore(1);
}
public static void main(String[] args) {
// 生產者先生產產品
new Thread(new Producer()).start();
// 消費者消費產品
new Thread(new Consumer()).start();
}
public static class Producer implements Runnable{
@Override
public void run() {
while(true) {
try {
empty.acquire();
mutex.acquire();
count++;
System.out.println("生產者生產了一個產品,現在有"+count+"產品");
mutex.release();
full.release();
Thread.sleep((int)Math.random()%10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
// 消費者主體代碼
public static class Consumer implements Runnable{
@Override
public void run() {
try {
full.acquire();
mutex.acquire();
count--;
System.out.println("消費者消費了一個產品,目前還剩下"+count+"個產品");
mutex.release();
empty.release();
Thread.sleep((int)Math.random()%10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}