简体   繁体   中英

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM