From 0d04dd505190d959e6d2cde16da54b2cb638699a Mon Sep 17 00:00:00 2001 From: irengrig Date: Tue, 5 Jul 2011 13:35:41 +0400 Subject: [PATCH] VCS: producer-consumer. Not used now (no performance increase) but maybe would be useful --- .../com/intellij/util/ProducerConsumer.java | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 platform/util/src/com/intellij/util/ProducerConsumer.java diff --git a/platform/util/src/com/intellij/util/ProducerConsumer.java b/platform/util/src/com/intellij/util/ProducerConsumer.java new file mode 100644 index 000000000000..7395bdb72943 --- /dev/null +++ b/platform/util/src/com/intellij/util/ProducerConsumer.java @@ -0,0 +1,92 @@ +/* + * Copyright 2000-2011 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.util; + +import java.util.ArrayDeque; + +/** + * @author irengrig + * Date: 7/5/11 + * Time: 1:48 AM + */ +public class ProducerConsumer { + public static final int ourDefaultMaxSize = 20; + + private final ArrayDeque myQueue; + private final Consumer myConsumer; + private final int myMaxSize; + private final Object myLock; + private final Thread myConsumerThread; + private boolean myIsAlive; + + public ProducerConsumer(final Consumer consumer) { + this(consumer, ourDefaultMaxSize); + } + + public void start() { + myIsAlive = true; + myConsumerThread.start(); + } + + public void stop() { + synchronized (myLock) { + myIsAlive = false; + myLock.notifyAll(); + } + } + + public ProducerConsumer(final Consumer consumer, final int maxSize) { + myConsumer = consumer; + myQueue = new ArrayDeque(); + myMaxSize = maxSize; + myLock = new Object(); + myConsumerThread = new Thread(new Runnable() { + @Override + public void run() { + synchronized (myLock) { + while (myIsAlive) { + if (! myQueue.isEmpty()) { + myConsumer.consume(myQueue.removeFirst()); + } else { + try { + myLock.wait(10); + } + catch (InterruptedException e) { + // + } + } + } + } + } + }); + } + + public void produce(final T t) { + synchronized (myLock) { + while (myQueue.size() >= myMaxSize) { + try { + myLock.notifyAll(); + myLock.wait(10); + } + catch (InterruptedException e) { + // + } + } + myQueue.addLast(t); + myLock.notifyAll(); + } + } +}