java.util.concurrent.BlockingDeque Java Examples

The following examples show how to use java.util.concurrent.BlockingDeque. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OutputProxy.java    From linstor-server with GNU General Public License v3.0 6 votes vote down vote up
public OutputProxy(
    final InputStream in,
    final BlockingDeque<Event> dequeRef,
    final byte delimiterPrm,
    final boolean useOutPrm
)
{
    dataIn = in;
    deque = dequeRef;
    delimiter = delimiterPrm;
    useOut = useOutPrm;
    data = new byte[INIT_DATA_SIZE];
    dataPos = 0;
    dataLimit = 0;

    shutdown = false;
}
 
Example #2
Source File: LinkedBlockingDequeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * takeLast() blocks interruptibly when empty
 */
public void testTakeLastFromEmptyBlocksInterruptibly() {
    final BlockingDeque q = new LinkedBlockingDeque();
    final CountDownLatch threadStarted = new CountDownLatch(1);
    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() {
            threadStarted.countDown();
            try {
                q.takeLast();
                shouldThrow();
            } catch (InterruptedException success) {}
            assertFalse(Thread.interrupted());
        }});

    await(threadStarted);
    assertThreadStaysAlive(t);
    t.interrupt();
    awaitTermination(t);
}
 
Example #3
Source File: LinkedBlockingDequeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * takeFirst() blocks interruptibly when empty
 */
public void testTakeFirstFromEmptyBlocksInterruptibly() {
    final BlockingDeque q = new LinkedBlockingDeque();
    final CountDownLatch threadStarted = new CountDownLatch(1);
    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() {
            threadStarted.countDown();
            try {
                q.takeFirst();
                shouldThrow();
            } catch (InterruptedException success) {}
            assertFalse(Thread.interrupted());
        }});

    await(threadStarted);
    assertThreadStaysAlive(t);
    t.interrupt();
    awaitTermination(t);
}
 
Example #4
Source File: LinkedBlockingDequeTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * takeLast() blocks interruptibly when empty
 */
public void testTakeLastFromEmptyBlocksInterruptibly() {
    final BlockingDeque q = new LinkedBlockingDeque();
    final CountDownLatch threadStarted = new CountDownLatch(1);
    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() {
            threadStarted.countDown();
            try {
                q.takeLast();
                shouldThrow();
            } catch (InterruptedException success) {}
            assertFalse(Thread.interrupted());
        }});

    await(threadStarted);
    assertThreadStaysAlive(t);
    t.interrupt();
    awaitTermination(t);
}
 
Example #5
Source File: LinkedBlockingDequeTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * takeFirst() blocks interruptibly when empty
 */
public void testTakeFirstFromEmptyBlocksInterruptibly() {
    final BlockingDeque q = new LinkedBlockingDeque();
    final CountDownLatch threadStarted = new CountDownLatch(1);
    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() {
            threadStarted.countDown();
            try {
                q.takeFirst();
                shouldThrow();
            } catch (InterruptedException success) {}
            assertFalse(Thread.interrupted());
        }});

    await(threadStarted);
    assertThreadStaysAlive(t);
    t.interrupt();
    awaitTermination(t);
}
 
Example #6
Source File: DynamicParsers.java    From type-parser with MIT License 6 votes vote down vote up
private <T> Collection<T> instantiateCollectionFromInterface(Class<? extends T> collectionType) {
    if (List.class.isAssignableFrom(collectionType)) {
        return new ArrayList<T>();
    } else if (SortedSet.class.isAssignableFrom(collectionType)) {
        return new TreeSet<T>();
    } else if (Set.class.isAssignableFrom(collectionType)) {
        return new LinkedHashSet<T>();
    } else if (BlockingDeque.class.isAssignableFrom(collectionType)) {
        return new LinkedBlockingDeque<T>();
    } else if (Deque.class.isAssignableFrom(collectionType)) {
        return new ArrayDeque<T>();
    } else if (BlockingQueue.class.isAssignableFrom(collectionType)) {
        return new LinkedBlockingDeque<T>();
    } else if (Queue.class.isAssignableFrom(collectionType)) {
        return new LinkedList<T>();
    }
    return new ArrayList<T>();
}
 
Example #7
Source File: YamlCollectionCreator.java    From Diorite with MIT License 6 votes vote down vote up
static void putAllCollections(Map<Class<?>, IntFunction<?>> map)
{
    safePut(map, ArrayList.class, ArrayList::new);
    safePut(map, HashSet.class, LinkedHashSet::new);
    safePut(map, Properties.class, x -> new Properties());
    safePut(map, Hashtable.class, Hashtable::new);

    safePut(map, Collection.class, ArrayList::new);
    safePut(map, Set.class, LinkedHashSet::new);
    safePut(map, List.class, ArrayList::new);
    safePut(map, SortedSet.class, x -> new TreeSet<>());
    safePut(map, Queue.class, x -> new ConcurrentLinkedQueue<>());
    safePut(map, Deque.class, x -> new ConcurrentLinkedDeque<>());
    safePut(map, BlockingQueue.class, x -> new LinkedBlockingQueue<>());
    safePut(map, BlockingDeque.class, x -> new LinkedBlockingDeque<>());


    safePut(map, HashMap.class, LinkedHashMap::new);
    safePut(map, LinkedHashMap.class, LinkedHashMap::new);
    safePut(map, ConcurrentHashMap.class, ConcurrentHashMap::new);

    safePut(map, Map.class, LinkedHashMap::new);
    safePut(map, ConcurrentMap.class, x -> new ConcurrentSkipListMap<>());
    safePut(map, ConcurrentNavigableMap.class, x -> new ConcurrentSkipListMap<>());
    safePut(map, SortedMap.class, i -> new TreeMap<>());
}
 
Example #8
Source File: StreamsBuilder.java    From replicator with Apache License 2.0 6 votes vote down vote up
private StreamsBuilder(
        int threads,
        int tasks,
        BiFunction<Input, Integer, Integer> partitioner,
        Class<? extends BlockingDeque> queueType,
        int queueSize,
        long queueTimeout,
        Function<Integer, Input> dataSupplierFn,
        Predicate<Input> filter,
        Function<Input, Output> process,
        Function<Output, Boolean> to,
        BiConsumer<Input, Integer> post) {
    this.threads = threads;
    this.tasks = tasks;
    this.partitioner = partitioner;
    this.queueType = queueType;
    this.queueSize = queueSize;
    this.queueTimeout = queueTimeout;
    this.dataSupplierFn = dataSupplierFn;
    this.filter = filter;
    this.process = process;
    this.to = to;
    this.post = post;
}
 
Example #9
Source File: SharedMemoryQueueResource.java    From baleen with Apache License 2.0 6 votes vote down vote up
/**
 * Create a (blocking) supplier for the given topic
 *
 * @param topic
 * @return the supplier
 */
public Supplier<String> createBlockingSupplier(final String topic) {
  final BlockingDeque<String> queue = getQueue(topic);
  return () -> {
    while (true) {
      try {
        final String s = queue.pollFirst(1, TimeUnit.MINUTES);
        if (s != null) {
          return s;
        }
      } catch (final InterruptedException e) {
        Thread.currentThread().interrupt();
      }
    }
  };
}
 
Example #10
Source File: DefaultCommitter.java    From hermes with Apache License 2.0 5 votes vote down vote up
private PartitionOperation<T> mergeMoreOperation(PartitionOperation<T> op,
      BlockingDeque<PartitionOperation<T>> opQueue, int maxRecords) {

	while (!opQueue.isEmpty() && op.getRecords().size() < maxRecords) {
		if (op.getRecords().size() + opQueue.peek().getRecords().size() <= maxRecords) {
			PartitionOperation<T> moreOp = opQueue.poll();
			op.merge(moreOp);
		} else {
			break;
		}
	}

	return op;
}
 
Example #11
Source File: AggregateContainer.java    From reef with Apache License 2.0 5 votes vote down vote up
AggregateContainer(final HeartBeatTriggerManager heartBeatTriggerManager,
                   final KryoUtils kryoUtils,
                   final BlockingDeque<byte[]> workerReportsQueue,
                   final TaskletAggregationRequest taskletAggregationRequest) {
  this.heartBeatTriggerManager = heartBeatTriggerManager;
  this.kryoUtils = kryoUtils;
  this.workerReportsQueue = workerReportsQueue;
  this.taskletAggregationRequest = taskletAggregationRequest;
}
 
Example #12
Source File: InstallationLogContainer.java    From redis-manager with Apache License 2.0 5 votes vote down vote up
public static List<String> getLogs(String clusterName) {
    List<String> logs = new LinkedList<>();
    BlockingDeque<String> logContainer = getLogDeque(clusterName);
    try {
        while (!logContainer.isEmpty()) {
            String log = logContainer.pollFirst(1, TimeUnit.SECONDS);
            if (!Strings.isNullOrEmpty(log)) {
                logs.add(log);
            }
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return logs;
}
 
Example #13
Source File: LinkedBlockingDequeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * takeFirst() throws InterruptedException immediately if interrupted
 * before waiting
 */
public void testTakeFirstFromEmptyAfterInterrupt() {
    final BlockingDeque q = new LinkedBlockingDeque();
    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() {
            Thread.currentThread().interrupt();
            try {
                q.takeFirst();
                shouldThrow();
            } catch (InterruptedException success) {}
            assertFalse(Thread.interrupted());
        }});

    awaitTermination(t);
}
 
Example #14
Source File: InstallationLogContainer.java    From redis-manager with Apache License 2.0 5 votes vote down vote up
private static BlockingDeque<String> getLogDeque(String clusterName) {
    BlockingDeque<String> logDeque = INSTALLATION_LOG.get(clusterName);
    if (logDeque == null) {
        logDeque = new LinkedBlockingDeque<>();
    }
    INSTALLATION_LOG.put(clusterName, logDeque);
    return logDeque;
}
 
Example #15
Source File: SharedMemoryQueueResource.java    From baleen with Apache License 2.0 5 votes vote down vote up
private synchronized BlockingDeque<String> getOrCreateQueue(final String topic) {
  BlockingDeque<String> queue = queues.get(topic);
  if (queue == null) {
    queue = new LinkedBlockingDeque<>(queueCapacity);
    queues.put(topic, queue);
  }
  return queue;
}
 
Example #16
Source File: LinkedBlockingDequeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * takeLast() throws InterruptedException immediately if interrupted
 * before waiting
 */
public void testTakeLastFromEmptyAfterInterrupt() {
    final BlockingDeque q = new LinkedBlockingDeque();
    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() {
            Thread.currentThread().interrupt();
            try {
                q.takeLast();
                shouldThrow();
            } catch (InterruptedException success) {}
            assertFalse(Thread.interrupted());
        }});

    awaitTermination(t);
}
 
Example #17
Source File: RawHttpDuplexOptions.java    From rawhttp with Apache License 2.0 5 votes vote down vote up
public RawHttpDuplexOptions(RawHttpClient<?> client, Duration pingPeriod,
                            ScheduledExecutorService pingScheduler,
                            Supplier<BlockingDeque<MessageSender.Message>> messageQueueFactory) {
    this.client = client;
    this.pingPeriod = pingPeriod;
    this.pingScheduler = pingScheduler;
    this.messageQueueFactory = messageQueueFactory;
}
 
Example #18
Source File: RawHttpDuplexOptions.java    From rawhttp with Apache License 2.0 5 votes vote down vote up
/**
 * Build a {@link RawHttpDuplexOptions} instance with the configured options.
 *
 * @return new instance
 */
public RawHttpDuplexOptions build() {
    RawHttpClient<?> client = getOrDefault(this.client, TcpRawHttpClient::new);
    Duration pingPeriod = getOrDefault(this.pingPeriod, () -> Duration.ofSeconds(5));
    ScheduledExecutorService pinger = getOrDefault(this.pingScheduler, Executors::newSingleThreadScheduledExecutor);
    Supplier<BlockingDeque<MessageSender.Message>> messageQueueFactory = getOrDefault(
            this.messageQueueFactory, Builder::defaultMessageQueue);
    return new RawHttpDuplexOptions(client, pingPeriod, pinger, messageQueueFactory);
}
 
Example #19
Source File: ChannelStateWriteRequestExecutorImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
ChannelStateWriteRequestExecutorImpl(
		String taskName,
		ChannelStateWriteRequestDispatcher dispatcher,
		BlockingDeque<ChannelStateWriteRequest> deque) {
	this.taskName = taskName;
	this.dispatcher = dispatcher;
	this.deque = deque;
	this.thread = new Thread(this::run, "Channel state writer " + taskName);
	this.thread.setDaemon(true);
}
 
Example #20
Source File: LinkedBlockingDequeTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * takeLast() throws InterruptedException immediately if interrupted
 * before waiting
 */
public void testTakeLastFromEmptyAfterInterrupt() {
    final BlockingDeque q = new LinkedBlockingDeque();
    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() {
            Thread.currentThread().interrupt();
            try {
                q.takeLast();
                shouldThrow();
            } catch (InterruptedException success) {}
            assertFalse(Thread.interrupted());
        }});

    awaitTermination(t);
}
 
Example #21
Source File: SharedMemoryQueueResource.java    From baleen with Apache License 2.0 5 votes vote down vote up
/**
 * Create a blocking consumer for the given topic
 *
 * @param topic
 * @return the consumer
 */
public Consumer<String> createBlockingConsumer(final String topic) {
  final BlockingDeque<String> queue = getQueue(topic);
  return t -> {
    boolean accepted = false;
    while (!accepted) {
      try {
        accepted = queue.offerLast(t, 1, TimeUnit.MINUTES);
      } catch (final InterruptedException e) {
        Thread.currentThread().interrupt();
      }
    }
  };
}
 
Example #22
Source File: LinkedBlockingDequeTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * takeFirst() throws InterruptedException immediately if interrupted
 * before waiting
 */
public void testTakeFirstFromEmptyAfterInterrupt() {
    final BlockingDeque q = new LinkedBlockingDeque();
    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() {
            Thread.currentThread().interrupt();
            try {
                q.takeFirst();
                shouldThrow();
            } catch (InterruptedException success) {}
            assertFalse(Thread.interrupted());
        }});

    awaitTermination(t);
}
 
Example #23
Source File: JournalFileRepositoryOrderTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testOrder() throws Throwable {
   ExecutorService executorService = Executors.newFixedThreadPool(3, new ActiveMQThreadFactory("test", false, JournalFileRepositoryOrderTest.class.getClassLoader()));
   final AtomicBoolean running = new AtomicBoolean(true);
   Thread t = null;
   try {
      FakeSequentialFileFactory fakeSequentialFileFactory = new FakeSequentialFileFactory();
      JournalImpl journal = new JournalImpl(new OrderedExecutorFactory(executorService), 10 * 1024, 2, -1, -1, 0, fakeSequentialFileFactory, "file", "file", 1, 0);

      final JournalFilesRepository repository = journal.getFilesRepository();
      final BlockingDeque<JournalFile> dataFiles = new LinkedBlockingDeque<>();


      // this is simulating how compating would return files into the journal
      t = new Thread() {
         @Override
         public void run() {
            while (running.get()) {
               try {
                  Wait.waitFor(() -> !running.get() || dataFiles.size() > 10, 1000, 1);
                  while (running.get()) {
                     JournalFile file = dataFiles.poll();
                     if (file == null) break;
                     repository.addFreeFile(file, false);
                  }
               } catch (Throwable e) {
                  e.printStackTrace();
               }
            }
         }
      };
      t.start();
      JournalFile file = null;
      LinkedList<Integer> values = new LinkedList<>();
      for (int i = 0; i < 5000; i++) {
         file = repository.openFile();
         Assert.assertNotNull(file);
         values.add(file.getRecordID());
         dataFiles.push(file);
      }

      int previous = Integer.MIN_VALUE;
      for (Integer v : values) {
         Assert.assertTrue(v.intValue() > previous);
         previous = v;
      }

   } finally {
      running.set(false);
      executorService.shutdownNow();
   }

}
 
Example #24
Source File: DaemonHandler.java    From linstor-server with GNU General Public License v3.0 4 votes vote down vote up
public DaemonHandler(final BlockingDeque<Event> dequeRef, final String... command)
{
    deque = dequeRef;
    processBuilder = new ProcessBuilder(command);
    processBuilder.redirectError(Redirect.PIPE);
}
 
Example #25
Source File: PacketQueueUDPHandler.java    From datacollector with Apache License 2.0 4 votes vote down vote up
public BlockingDeque<DatagramPacket> getPacketQueue() {
  return queue;
}
 
Example #26
Source File: Test.java    From banyan with MIT License 4 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    BlockingDeque<String> queue = new LinkedBlockingDeque<>();

    Producer p1 = new Producer(queue);
    Producer p2 = new Producer(queue);

    Consumer c1 = new Consumer(queue);
    Consumer c2 = new Consumer(queue);

    ExecutorService executorService = Executors.newCachedThreadPool();

    executorService.execute(p1);
    executorService.execute(p2);
    executorService.execute(c1);
    executorService.execute(c2);

    Thread.sleep(1000);

    p1.stop();
    p2.stop();

    executorService.shutdownNow();

}
 
Example #27
Source File: Producer.java    From banyan with MIT License 4 votes vote down vote up
public Producer(BlockingDeque<String> queue) {
    this.queue = queue;
}
 
Example #28
Source File: MockRabbitMQResource.java    From baleen with Apache License 2.0 4 votes vote down vote up
public BlockingDeque<byte[]> getSupply() {
  return supply;
}
 
Example #29
Source File: AbstractDepsAwareExecutor.java    From buck with Apache License 2.0 4 votes vote down vote up
protected AbstractDepsAwareExecutor(
    BlockingDeque<TaskType> workQueue, Future<?>[] workers, ExecutorService executorService) {
  this.workQueue = workQueue;
  this.workers = workers;
  this.executorService = executorService;
}
 
Example #30
Source File: DefaultDepsAwareExecutorWithLocalStack.java    From buck with Apache License 2.0 4 votes vote down vote up
private DefaultDepsAwareExecutorWithLocalStack(
    BlockingDeque<DefaultDepsAwareTask<T>> workQueue,
    Future<?>[] workers,
    ExecutorService ownThreadPool) {
  super(workQueue, workers, ownThreadPool);
}