baklap
baklap

Reputation: 2173

Dynamic buffer in java

What I'm looking for is some sort of dynamic buffering which I try to explain:

It should be possible to add some values at the end of the buffer. And while it's adding up values at the end of the buffer, I should be able to get some values from the beginning of the buffer.

So like this in pseudocode:

ThreadA
while(true):
   buffer.pushToEnd(random);

ThreadB
while(true):
   buffer.popFromBeginning();

So the values I'm popping should be removed from the buffer. I'm looking all over the internet and found various buffer types, but none of them is capable of acting this way.

So my question is: Is there some native java implementation of this buffer behavior? Or some example code of the implementation?

Upvotes: 0

Views: 283

Answers (2)

rath
rath

Reputation: 4089

You are describing a FIFO queue data structure.

Java does include such a container class with exactly the methods you want. Also take a look at the thread-safe variant ConcurrentLinkedQueue.

Moreover what you want to do looks a lot like the producer-consumer problem. The Wikipedia page linked may be a good starting point to go about it.

Upvotes: 3

Josh Engelsma
Josh Engelsma

Reputation: 2646

You should use a Java Queue A FIFO data container. The first thing you added to the container is what you are popping. First in, first out.

Upvotes: 1

Related Questions