我试图通过使用ConcurrentHashMap和newFixedThreadPool来解决多线程的消费者 - 生产者问题。
我的目标是让不同的线程同时放入和删除hashmap并确保地图大小不会大于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);
}
}
}
答案 0 :(得分:0)
您可以使用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();
}
}
}