i want thread-safe container blocks caller until item becomes available. items added @ rate of 1000s per second container not drained same rate. therefore, want container disallow duplicates. wrote simple wrapper around linkedblockingqueue realized i've recreated classic producer-consumer deadlock. had written:
public class activitylistener { private final blockingqueue<id> activeitems = new linkedblockingqueue<>(); public synchronized id take() throws interruptedexception { return activeitems.take(); } public synchronized void registeractivity(final id item) { if (!activeitems.contains(item)) { activeitems.add(item); } } public synchronized boolean isitemactive(final id item) { return activeitems.contains(item); } } i not find established solution problem , appreciate help.
override add() , put() methods of implementation of blockingqueue check first if element within queue.
something -
@override public boolean add(t obj) { if (contains(obj)) return true; return super.add(obj); }
Comments
Post a Comment