Advertisement
STANAANDREY

Concurent IdGetter java

Feb 4th, 2022 (edited)
871
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.57 KB | None | 0 0
  1. class IdGetter {
  2.     private AtomicLong curr;
  3.     private AtomicReference<Queue<Long>> priorityQueue;
  4.  
  5.     public IdGetter(final long start) {
  6.         priorityQueue = new AtomicReference<>(new PriorityQueue<>());
  7.         curr = new AtomicLong(start);
  8.     }
  9.  
  10.     public void remove(final long id) {
  11.         if (id <= curr.get()) {
  12.             priorityQueue.get().add(id);
  13.         }
  14.     }
  15.  
  16.     public long get() {
  17.         if (!priorityQueue.get().isEmpty()) {
  18.             return priorityQueue.get().poll();
  19.         }
  20.         return curr.getAndIncrement();
  21.     }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement