更新時(shí)間:2023-08-16 來(lái)源:黑馬程序員 瀏覽量:
IT就到黑馬程序員.gif)
生產(chǎn)者-消費(fèi)者問(wèn)題是一個(gè)經(jīng)典的并發(fā)編程問(wèn)題,涉及到多個(gè)線程共享資源的同步與互斥。使用Java的wait和notify機(jī)制可以有效地解決這個(gè)問(wèn)題。
  接下來(lái)筆者就用具體的Java代碼實(shí)現(xiàn)的簡(jiǎn)單生產(chǎn)者-消費(fèi)者問(wèn)題解決方案:
import java.util.LinkedList;
class SharedResource {
    private LinkedList<Integer> buffer = new LinkedList<>();
    private int capacity = 5;
    public synchronized void produce() throws InterruptedException {
        while (buffer.size() == capacity) {
            wait(); // 等待,直到緩沖區(qū)不滿
        }
        int item = (int) (Math.random() * 100);
        buffer.add(item);
        System.out.println("Produced: " + item);
        notifyAll(); // 喚醒等待的消費(fèi)者線程
    }
    public synchronized void consume() throws InterruptedException {
        while (buffer.size() == 0) {
            wait(); // 等待,直到緩沖區(qū)不空
        }
        int item = buffer.removeFirst();
        System.out.println("Consumed: " + item);
        notifyAll(); // 喚醒等待的生產(chǎn)者線程
    }
}
class Producer implements Runnable {
    private SharedResource sharedResource;
    public Producer(SharedResource sharedResource) {
        this.sharedResource = sharedResource;
    }
    @Override
    public void run() {
        try {
            while (true) {
                sharedResource.produce();
                Thread.sleep(1000); // 模擬生產(chǎn)時(shí)間
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
class Consumer implements Runnable {
    private SharedResource sharedResource;
    public Consumer(SharedResource sharedResource) {
        this.sharedResource = sharedResource;
    }
    @Override
    public void run() {
        try {
            while (true) {
                sharedResource.consume();
                Thread.sleep(1500); // 模擬消費(fèi)時(shí)間
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
public class Main {
    public static void main(String[] args) {
        SharedResource sharedResource = new SharedResource();
        Thread producerThread = new Thread(new Producer(sharedResource));
        Thread consumerThread = new Thread(new Consumer(sharedResource));
        producerThread.start();
        consumerThread.start();
    }
}在這個(gè)例子中,SharedResource類表示共享的緩沖區(qū),其中的produce和consume方法分別用于生產(chǎn)和消費(fèi)數(shù)據(jù)。Producer和Consumer類分別表示生產(chǎn)者和消費(fèi)者線程,它們通過(guò)調(diào)用SharedResource的方法來(lái)生產(chǎn)和消費(fèi)數(shù)據(jù)。使用wait和notifyAll方法來(lái)實(shí)現(xiàn)線程之間的同步與互斥,確保在正確的時(shí)機(jī)喚醒等待的線程。
需要注意的是,這只是一個(gè)簡(jiǎn)單的示例,實(shí)際生產(chǎn)環(huán)境中可能需要更復(fù)雜的邏輯和錯(cuò)誤處理。同時(shí),Java 5及之后的版本提供了更高級(jí)的并發(fā)工具,如BlockingQueue和ExecutorService,可以更方便地解決生產(chǎn)者-消費(fèi)者問(wèn)題。
Elasticsearch在部署時(shí),對(duì)Linux的設(shè)置有哪些優(yōu)化方法?
2023-08-15服務(wù)上線怎么不影響舊版本?
2023-08-14SpringApplication實(shí)例的初始化和項(xiàng)目初始化啟動(dòng)
2023-08-11a.hashCode()有什么用?與a.equals(b)有什么關(guān)系?
2023-08-11哪個(gè)類包含clone方法?是Cloneable還是Object?
2023-08-10Java培訓(xùn)出來(lái)的好找工作嗎?能拿多少工資?
2023-08-09