Circular Buffer 的线程安全
我想确保我的循环缓冲区是线程安全的。我使用缓冲区来存储通过蓝牙传输的数据,同时我使用另一个线程来删除数据并存储在本地 Android 设备中。
这是我当前使用 Semaphore
的 CircularBuffer
。是否可以通过简单地将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为你只需要一个互斥访问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.