Which concurrent Queue implementation should I use in Java?

ConcurrentLinkedQueue means no locks are taken (i.e. no synchronized(this) or Lock.lock calls). It will use a CAS – Compare and Swap operation during modifications to see if the head/tail node is still the same as when it started. If so, the operation succeeds. If the head/tail node is different, it will spin around and try … Read more

Run PHP Task Asynchronously

I’ve used the queuing approach, and it works well as you can defer that processing until your server load is idle, letting you manage your load quite effectively if you can partition off “tasks which aren’t urgent” easily. Rolling your own isn’t too tricky, here’s a few other options to check out: GearMan – this … Read more

Is there a fixed sized queue which removes excessive elements?

Actually the LinkedHashMap does exactly what you want. You need to override the removeEldestEntry method. Example for a queue with max 10 elements: queue = new LinkedHashMap<Integer, String>() { @Override protected boolean removeEldestEntry(Map.Entry<Integer, String> eldest) { return this.size() > 10; } }; If the “removeEldestEntry” returns true, the eldest entry is removed from the map.

Fixed size queue which automatically dequeues old values upon new enques

I would write a wrapper class that on Enqueue would check the Count and then Dequeue when the count exceeds the limit. public class FixedSizedQueue<T> { ConcurrentQueue<T> q = new ConcurrentQueue<T>(); private object lockObject = new object(); public int Limit { get; set; } public void Enqueue(T obj) { q.Enqueue(obj); lock (lockObject) { T overflow; … Read more

How do I instantiate a Queue object in java?

Queue is an interface. You can’t instantiate an interface directly except via an anonymous inner class. Typically this isn’t what you want to do for a collection. Instead, choose an existing implementation. For example: Queue<Integer> q = new LinkedList<Integer>(); or Queue<Integer> q = new ArrayDeque<Integer>(); Typically you pick a collection implementation by the performance and … Read more

Creating a blocking Queue in .NET?

That looks very unsafe (very little synchronization); how about something like: class SizeQueue<T> { private readonly Queue<T> queue = new Queue<T>(); private readonly int maxSize; public SizeQueue(int maxSize) { this.maxSize = maxSize; } public void Enqueue(T item) { lock (queue) { while (queue.Count >= maxSize) { Monitor.Wait(queue); } queue.Enqueue(item); if (queue.Count == 1) { // … Read more

Size-limited queue that holds last N elements in Java

Apache commons collections 4 has a CircularFifoQueue<> which is what you are looking for. Quoting the javadoc: CircularFifoQueue is a first-in first-out queue with a fixed size that replaces its oldest element if full. import java.util.Queue; import org.apache.commons.collections4.queue.CircularFifoQueue; Queue<Integer> fifo = new CircularFifoQueue<Integer>(2); fifo.add(1); fifo.add(2); fifo.add(3); System.out.println(fifo); // Observe the result: // [2, 3] If … Read more

Queue.Queue vs. collections.deque

Queue.Queue and collections.deque serve different purposes. Queue.Queue is intended for allowing different threads to communicate using queued messages/data, whereas collections.deque is simply intended as a datastructure. That’s why Queue.Queue has methods like put_nowait(), get_nowait(), and join(), whereas collections.deque doesn’t. Queue.Queue isn’t intended to be used as a collection, which is why it lacks the likes … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)