2 * Copyright (C) 2019 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.cdm
.common
.concurrent
;
11 import java
.util
.LinkedList
;
12 import java
.util
.List
;
14 import org
.apache
.logging
.log4j
.LogManager
;
15 import org
.apache
.logging
.log4j
.Logger
;
18 * Concurrent queue. Example taken from
20 * https://stackoverflow.com/questions/33195290/producer-consumer-model-using-javasynchronized-but-always-run-the-same-thread
21 * } for Producer-Consumer pattern.
26 public class ConcurrentQueue
<T
> {
28 private static final Logger logger
= LogManager
.getLogger();
31 private List
<T
> queue
= new LinkedList
<>();
33 public ConcurrentQueue(int capacity
) {
34 this.capacity
= capacity
;
37 public synchronized void enqueue(T item
) throws InterruptedException
{
38 while (queue
.size() == this.capacity
) {
42 // System.out.println("Thread " + Thread.currentThread().getName() +
43 // " producing " + item);
47 if (queue
.size() >= 1) {
52 public synchronized T
dequeue() throws InterruptedException
{
55 while (queue
.size() == 0) {
59 item
= queue
.remove(0);
61 // System.out.println("Thread " + Thread.currentThread().getName() +
62 // " consuming " + item);
64 if (queue
.size() == (capacity
- 1)) {
74 private void printSize() {
75 logger
.warn("size:" + queue
.size());