001package co.codewizards.cloudstore.core.concurrent;
002
003import java.util.concurrent.RejectedExecutionException;
004import java.util.concurrent.RejectedExecutionHandler;
005import java.util.concurrent.ThreadPoolExecutor;
006
007/**
008 * A handler for rejected tasks that blocks until the rejected task
009 * can be added to the queue.
010 * <p>
011 * The behaviour of an application using this policy is similar to one using the
012 * {@link ThreadPoolExecutor.CallerRunsPolicy},
013 * but instead of running a certain {@code Callable} on the main thread, which may block this thread
014 * extremely long - much longer than the other {@code Callable}s run, the main thread already continues
015 * as soon as any of the currently running tasks finished.
016 * @author Marco หงุ่ยตระกูล-Schulze - marco at codewizards dot co
017 */
018public class CallerBlocksPolicy implements RejectedExecutionHandler {
019    @Override
020        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
021        try {
022                        e.getQueue().put(r);
023                } catch (InterruptedException x) {
024                        throw new RejectedExecutionException(x);
025                }
026    }
027}