Last active
August 29, 2015 14:18
-
-
Save bassemZohdy/7ca3c940bf5ce2feda14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.LinkedBlockingQueue; | |
import java.util.concurrent.atomic.AtomicBoolean; | |
import java.util.function.Consumer; | |
import java.util.function.Supplier; | |
public class BlockedForEachLinkedBlockingQueue<T> extends LinkedBlockingQueue<T> { | |
private static final long serialVersionUID = 9053130343708158526L; | |
private static final AtomicBoolean stopBlockedForEach = new AtomicBoolean(false); | |
public void stopBlockedForEach() { | |
stopBlockedForEach.set(true); | |
} | |
public void blockedForEachWrappingException(Consumer<? super T> action) { | |
Consumer<T> consumer = t -> action.accept(t); | |
Supplier<T> supplier = () -> takeWrappingToUncatchedException(); | |
while (!stopBlockedForEach.get()) { | |
consumer.accept(supplier.get()); | |
} | |
} | |
private T takeWrappingToUncatchedException() { | |
try { | |
return take(); | |
} catch (InterruptedException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment