Search code examples
javaapache-commonsapache-commons-collection

Proper way to work with FIFO buffer


I have this FIFO which I'm going to use to store data from network server:

 Buffer nServerFifo = (Buffer) BufferUtils.synchronizedBuffer(new CircularFifoBuffer(200));

    // Insert into to the Network Server Buffer    
    public void nServerFifoAdd(String string){
        nServerFifo.add(string);        
    }

    // Get data from the Network Server Buffer
    public Object nServerFifoGet(){         
        Object string = nServerFifo.get();    
        nServerFifo.remove(string);
        return string;           
    }

My question is what is the proper way to store data insert and get data from the buffer? Do I need to delete the data after I get if or this is done by the buffer? Do you have Idea what is the maximum String length size that I can store into the buffer?


Solution

  • Its Better use ArrayBlockingQueue class which is present in java.util.concurrent package, which is Thread Safe.

    BlockingQueue<String> queue = new ArrayBlockingQueue<String>(100);
    
    queue.put("Vivek");   // To insert value into Queue
    
    queue.take();         // To take the value out of Queue