Java bounded non-blocking buffer for high concurrent situation

前端 未结 5 1814
小蘑菇
小蘑菇 2021-02-06 05:25

Basically I need a data structure to store the temporary chatting messages on the server side. It should be:

  • bounded: because I don\'t need store too many messa

5条回答
  •  一个人的身影
    2021-02-06 05:58

    You can use LinkedBlockingQueue with the non-blocking methods offer (or add) and poll to access it. You can create it with a fixed capacity to make it bounded.

    LinkedBlockingQueue myStrings = new LinkedBlockingQueue(100);
    myStrings.offer("Hi!"); // returns false if limit is reached
    myStrings.add("Hi, again!"); // throws exception if limit is reached
    String s = myStrings.poll(); // returns null if queue is empty
    

提交回复
热议问题