Loading

[Java手撕]线程安全的循环队列

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class CircularQueue<T> {
    private final T[] items;
    private final int capacity;
    private int count;
    private int head;
    private int tail;
    
    private final Lock lock = new ReentrantLock();

    @SuppressWarnings("unchecked")
    public CircularQueue(int capacity) {
        if (capacity <= 0) {
            throw new IllegalArgumentException("Capacity must be positive");
        }
        this.capacity = capacity;
        this.items = (T[]) new Object[capacity];
        this.count = 0;
        this.head = 0;
        this.tail = 0;
    }

    public boolean enqueue(T item) {
        lock.lock();
        try {
            if (count == capacity) {
                return false; // 队列已满
            }
            items[tail] = item;
            tail = (tail + 1) % capacity;
            count++;
            return true;
        } finally {
            lock.unlock();
        }
    }

    public T dequeue() {
        lock.lock();
        try {
            if (count == 0) {
                return null; // 队列为空
            }
            T item = items[head];
            items[head] = null; // 帮助GC
            head = (head + 1) % capacity;
            count--;
            return item;
        } finally {
            lock.unlock();
        }
    }

    public int size() {
        lock.lock();
        try {
            return count;
        } finally {
            lock.unlock();
        }
    }

    public boolean isEmpty() {
        lock.lock();
        try {
            return count == 0;
        } finally {
            lock.unlock();
        }
    }

    public boolean isFull() {
        lock.lock();
        try {
            return count == capacity;
        } finally {
            lock.unlock();
        }
    }
}
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.locks.ReentrantLock;

public class MyCircularQueue {

    private ReentrantLock lock;
    private final int capacity;
    private int count;
    private final int[] queue;
    private int head;
    private int tail;

    public MyCircularQueue(int k){
        lock = new ReentrantLock();
        this.queue =  new int[k];
        this.capacity = k;
        this.count = 0;
        this.head = 0;
        this.tail = 0;
    }

    public boolean enQueue(int value) {

        lock.lock();
        try{
            if(count>=capacity){
                return false;
            }
            queue[tail] = value;
            tail = (tail+1)%capacity;
            count += 1;

        }finally {
            lock.unlock();
        }

        return true;

    }

    public boolean deQueue() {
        lock.lock();
        try{
            if(count<=0){
                return false;
            }
            head = (head+1)%capacity;
            count -= 1;
        }finally {
            lock.unlock();
        }
        return true;
    }

    public int Front() {
        if(isEmpty()){
            return -1;
        }
        return queue[head];
    }

    public int Rear() {
        if(isEmpty()){
            return -1;
        }
        return queue[(tail+capacity-1)%capacity];
    }

    public boolean isEmpty() {
        return count == 0;
    }

    public boolean isFull() {
        return count == capacity;
    }

    public void print(){
        System.out.println(Arrays.toString(queue));
        System.out.println(head+":"+tail);
    }
}
posted @ 2025-04-18 17:53  Duancf  阅读(15)  评论(0)    收藏  举报