簡體   English   中英

Java-使用ConcurrentHashMap通過多線程改進消費者生產者

[英]Java - Improving Consumer Producer with MultiThreading using ConcurrentHashMap

我試圖通過使用ConcurrentHashMap和newFixedThreadPool解決多線程的消費者生產者問題。

我的目標是使不同線程可以同時從哈希圖放置和刪除,並確保圖的大小不會大於MAXQUEUE(每個元素的唯一鍵)。

下面的程序的行為不符合我的期望,它會填滿地圖,直到大小為20,然后刪除20,依此類推。

我需要一些幫助使其成為描述,也很樂意得到改善代碼的建議。

這是我的生產者班級:

public class Producer extends Thread
{
static final int MAXQUEUE = 20;
private ConcurrentHashMap<Long, String> myMap = new ConcurrentHashMap<Long, String>();
private AtomicLong m_Key = new AtomicLong(0);

public void run() 
{
    try 
    {
        while (true) 
        {
            putMessage();
        }
    } catch (InterruptedException e) 
    {
    }
}

private void putMessage() throws InterruptedException 
{
    synchronized(this)
    {
        while (myMap.size() == MAXQUEUE) 
        {
            wait();
        }
        myMap.put(this.m_Key.incrementAndGet(), "Hello");
        System.out.println(Thread.currentThread().getName() + " put message; key " + this.m_Key);
        notify();
        //Later, when the necessary event happens, the thread that is running it calls notify() from a block synchronized on the same object.
    }
}

// Called by Consumer    
public void removeElementFromMap() throws InterruptedException
{
    synchronized(this)
    {
        notify();
        while (myMap.size() == 0)
        {
            wait();
        }

        for (Iterator<Entry<Long, String>> iter = this.myMap.entrySet().iterator() ; iter.hasNext() ; ) 
        {
            Map.Entry<Long, String> entry = iter.next();   
            System.out.println("Removed element with key " + entry.getKey() );
            iter.remove();
        }
    }
}

}

消費類:

public class Consumer extends Thread
{
Producer producer;


   public void Consumer(Producer p) 
{
    producer = p;
}

public void run() 
{
    try 
    {
        while (true) 
        {
            producer.removeElementFromMap();
        }
    } catch (InterruptedException e) 
    {
        e.printStackTrace();
    }
}

主類:

public class Main 
{

public static void main(String[] args) 
{
    Producer producer = new Producer();
    ExecutorService producersExecutors = Executors.newFixedThreadPool(5);
    Consumer consumer = new Consumer(producer);
    ExecutorService consumersExecutors = Executors.newFixedThreadPool(5);
    for(int i=0;i<5;i++)
    {
        producersExecutors.execute(producer);
        consumersExecutors.execute(consumer);
    }

}
}

您可以使用Java中可用的阻塞隊列,例如ArrayBlockingQueue。 但是,如果您仍然想使用地圖以及自己的處理方式,則可以按照以下方式進行操作-

public class MapTest {

    public static void main(String[] args) {
        DataStore dataStore = new DataStore(100);
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 5; i++) {
            executorService.execute(new Producer(dataStore));
            executorService.execute(new Consumer(dataStore));
        }
    }
}

class DataStore {

    private final int maxQueueSize;
    private Lock lock = new ReentrantLock();
    private AtomicInteger queueSize = new AtomicInteger(0);
    private AtomicLong keyGenerator = new AtomicLong(0);
    private Map<Long, String> map = new HashMap<Long, String>();

    public DataStore(int maxQueueSize) {
        this.maxQueueSize = maxQueueSize;
    }

    public void putMessage() throws InterruptedException {
        while (queueSize.get() == maxQueueSize) {
            Thread.sleep(10);
        }
        lock.lock();
        try {
            if (queueSize.get() < maxQueueSize) {
                map.put(keyGenerator.incrementAndGet(), "Hello");
                queueSize.incrementAndGet();
                System.out.println(Thread.currentThread().getName() + " put message; key " + keyGenerator.get() + ", queue size: " + queueSize.get());
            }
        } finally {
            lock.unlock();
        }
    }

    public void removeMessage() throws InterruptedException {
        while (queueSize.get() == 0) {
            Thread.sleep(10);
        }
        lock.lock();
        try {
            if (queueSize.get() > 0) {
                Iterator<Long> keyIterator = map.keySet().iterator();
                if (keyIterator.hasNext()) {
                    Long key = keyIterator.next();
                    map.remove(key);
                    queueSize.decrementAndGet();
                    System.out.println(Thread.currentThread().getName() + " removed message; key: " + key + ", queue size: " + queueSize.get());
                }
            }
        } finally {
            lock.unlock();
        }
    }
}

class Producer implements Runnable {

    private DataStore dataStore;

    public Producer(DataStore dataStore) {
        this.dataStore = dataStore;
    }

    public void run() {
        try {
            while (true) {
                dataStore.putMessage();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Consumer implements Runnable {
    private DataStore dataStore;

    public Consumer(DataStore dataStore) {
        this.dataStore = dataStore;
    }

    public void run() {
        try {
            while (true) {
                dataStore.removeMessage();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM