Circular Buffer 的线程安全

发布于 2025-01-05 18:44:36 字数 1671 浏览 1 评论 0原文

我想确保我的循环缓冲区是线程安全的。我使用缓冲区来存储通过蓝牙传输的数据,同时我使用另一个线程来删除数据并存储在本地 Android 设备中。

这是我当前使用 SemaphoreCircularBuffer。是否可以通过简单地将 synchronized 添加到每个方法来使其线程证明?这将是我的首选方法。

public class CircularBuffer {
// private byte[][] data;
private int data[];
private int head;
private int tail;
private Semaphore readPermission;

public CircularBuffer(Integer number) {
    // data = new byte[number][];
    readPermission = new Semaphore(1);
    data = new int[number];
    head = 0;
    tail = 0;
}

public boolean store(byte[] value) {
    if (!bufferFull()) {
        ByteBuffer bb = ByteBuffer.wrap(value);
        // may need to be reversed
        int intVal = bb.getShort();
        Log.i("Buffer Input", "" + intVal);
        // data[tail++] = value;
        data[tail++] = intVal;
        if (tail == data.length) {
            tail = 0;
        }
        return true;
    } else {
        return false;
    }
}

public int getSize() {
    return tail - head;
}

public int read() {
    Log.i("Buffer", "Taking");
    if (head != tail) {
        // byte[] value = data[head++];
        int value=data[head++];
        if (head == data.length) {
            head = 0;
        }
        return value;
    } else {
        //return null;
        return 0;
    }
}

//Getting permission using a semaphore
public void getPermission(){
    try {
        readPermission.acquire();
    } catch (InterruptedException e) {
        Log.i("Buffer", "Interrupted Exception");
        e.printStackTrace();
    }
}

//Giving up permission using a semaphore
public void givePersmission(){
    readPermission.release();
}

}

I would like to make sure that my circular buffer is thread safe. I am using the buffer to store data streamed in over bluetooth, and at the same time I am using another thread to remove data and store in locally in an android device.

This is my current CircularBuffer using Semaphore. Is it possible to make it thread proof by simply adding synchronized to every method? That would be my preferred method.

public class CircularBuffer {
// private byte[][] data;
private int data[];
private int head;
private int tail;
private Semaphore readPermission;

public CircularBuffer(Integer number) {
    // data = new byte[number][];
    readPermission = new Semaphore(1);
    data = new int[number];
    head = 0;
    tail = 0;
}

public boolean store(byte[] value) {
    if (!bufferFull()) {
        ByteBuffer bb = ByteBuffer.wrap(value);
        // may need to be reversed
        int intVal = bb.getShort();
        Log.i("Buffer Input", "" + intVal);
        // data[tail++] = value;
        data[tail++] = intVal;
        if (tail == data.length) {
            tail = 0;
        }
        return true;
    } else {
        return false;
    }
}

public int getSize() {
    return tail - head;
}

public int read() {
    Log.i("Buffer", "Taking");
    if (head != tail) {
        // byte[] value = data[head++];
        int value=data[head++];
        if (head == data.length) {
            head = 0;
        }
        return value;
    } else {
        //return null;
        return 0;
    }
}

//Getting permission using a semaphore
public void getPermission(){
    try {
        readPermission.acquire();
    } catch (InterruptedException e) {
        Log.i("Buffer", "Interrupted Exception");
        e.printStackTrace();
    }
}

//Giving up permission using a semaphore
public void givePersmission(){
    readPermission.release();
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多情出卖 2025-01-12 18:44:36

因为你只需要一个互斥访问synchronized就可以了,但你不需要在所有方法上使用它。只有真正需要同步的方法,
如果您使用信号量,请确保编写异常安全代码,以确保无论发生什么情况都会释放信号量。
从这个角度来看,互斥锁更安全。

as you need only a mutual exclusive access synchronized is OK, but you do not need to use it on all the methods. Only the methods that really need to be synch,
If you use semaphore be sure you write exceètion safe code to be sure that you will release the semaphore whatever happens.
From this point of view a mutex is safer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文