Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7494105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:19:05+00:00 2026-05-29T17:19:05+00:00

I would like to make sure that my circular buffer is thread safe. I

  • 0

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();
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T17:19:09+00:00Added an answer on May 29, 2026 at 5:19 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to make sure that an applescript can be converted to bash.
I am running a mysql server. I would like to somehow make sure that
I would like to make sure that a series of commands are executed in
I would like to make sure that everything I know about UTF-8 is correct.
I would like to make sure that when user log in it will stay
I would like to make sure that the list is scrolled all the way
I have a paragraph of text and would like to make sure that one
I would like to poll google every 10 seconds to make sure that there
I would like to make sure that a certain user isn't existing already in
I would like to make sure if a textbox has any content at all,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.