chatprogramm was sonst
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
757 B

package server.util;
/*
!!! WORK IN PROGRESS !!!
trying to implement a wait-free queue
*/
import java.util.concurrent.atomic.AtomicReferenceArray;
public class Queue<T> {
private volatile int head = 0, tail = 0;
private final int capacity;
private AtomicReferenceArray<T> items;
public Queue(int capacity) {
this.capacity = capacity;
items = new AtomicReferenceArray<>(capacity);
}
public boolean enq(T x) {
if (tail - head == capacity) return false;
items.set((tail + 1) % capacity, x);
tail++;
return true;
}
public T deq() {
if (tail - head == 0) return null;
T x = items.get((head + 1) % capacity);
head++;
return x;
}
}