Tôi đang cố tạo một bản thực thi hàng đợi không có khóa trong Java, chủ yếu cho việc học cá nhân. Hàng đợi phải là hàng đợi chung, cho phép bất kỳ số lượng người đọc và/hoặc người viết nào đồng thời.Đây có phải là (Khóa-Miễn phí) Thực thi Hàng đợi An toàn Chủ đề không?
Bạn có vui lòng xem lại và đề xuất bất kỳ cải thiện/vấn đề nào bạn tìm thấy không?
Cảm ơn bạn.
import java.util.concurrent.atomic.AtomicReference;
public class LockFreeQueue<T> {
private static class Node<E> {
E value;
volatile Node<E> next;
Node(E value) {
this.value = value;
}
}
private AtomicReference<Node<T>> head, tail;
public LockFreeQueue() {
// have both head and tail point to a dummy node
Node<T> dummyNode = new Node<T>(null);
head = new AtomicReference<Node<T>>(dummyNode);
tail = new AtomicReference<Node<T>>(dummyNode);
}
/**
* Puts an object at the end of the queue.
*/
public void putObject(T value) {
Node<T> newNode = new Node<T>(value);
Node<T> prevTailNode = tail.getAndSet(newNode);
prevTailNode.next = newNode;
}
/**
* Gets an object from the beginning of the queue. The object is removed
* from the queue. If there are no objects in the queue, returns null.
*/
public T getObject() {
Node<T> headNode, valueNode;
// move head node to the next node using atomic semantics
// as long as next node is not null
do {
headNode = head.get();
valueNode = headNode.next;
// try until the whole loop executes pseudo-atomically
// (i.e. unaffected by modifications done by other threads)
} while (valueNode != null && !head.compareAndSet(headNode, valueNode));
T value = (valueNode != null ? valueNode.value : null);
// release the value pointed to by head, keeping the head node dummy
if (valueNode != null)
valueNode.value = null;
return value;
}
Ported để Mã xét tại http://codereview.stackexchange.com/câu hỏi/224/là-này-lock-free-queue-thực hiện-thread-an toàn –