How to make an artist reject new tasks when starting another?

Using this artist

executor = Executors.newFixedThreadPool(1);

Only one task will be launched at a time, but will sequentially perform any subsequent tasks. I want him to reject new tasks if he is already running. Should I use a different type of artist? Which one of? Or should I look for a different approach here?

+4
source share
2 answers

You can do this with ThreadPoolExecutor with kernel size and maximum pool 1.

, SynchronousQueue ( ), , , , RejectedExecutionException.

( ):

new ThreadPoolExecutor(1, 1, 1L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());

, - . DiscardPolicy, .

, . corepool maxpool, .

+6

:

ThreadPoolExecutor executor= new ThreadPoolExecutor(1,1,
                    60,TimeUnit.SECONDS,
                    new ArrayBlockingQueue<Runnable>(1),
                    new ThreadPoolExecutor.DiscardPolicy());

:

ThreadPoolExecutor DiscardPolicy

+1

Source: https://habr.com/ru/post/1657861/


All Articles