ThreadPoolExecutor.CallerRunsPolicy in Java
November 15, 2023
1. ThreadPoolExecutor.CallerRunsPolicy
belongs to java.util.concurrent package. It is introduced in Java 5.2.
ThreadPoolExecutor.CallerRunsPolicy
is the handler for rejected task. ThreadPoolExecutor.CallerRunsPolicy
executes the rejected task itself.3.
ThreadPoolExecutor.CallerRunsPolicy
has a method rejectedExecution(Runnable r, ThreadPoolExecutor e) which accepts a thread and ThreadPoolExecutor
. ThreadPoolExecutor.CallerRunsPolicy
is passed as an argument while initializing ThreadPoolExecutor
.
ThreadPoolExecutor tpool= new ThreadPoolExecutor(2,3,500, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new ThreadPoolExecutor.CallerRunsPolicy());
Example
CallerRunsPolicyDemo.javapackage com.concretepage; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class CallerRunsPolicyDemo { class DemoThread implements Runnable { public void run() { System.out.println("Hello World"); } } public static void main(String... args) { Runnable th=new CallerRunsPolicyDemo().new DemoThread(); ThreadPoolExecutor tpool= new ThreadPoolExecutor(2,3,500, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new ThreadPoolExecutor.CallerRunsPolicy()); tpool.execute(th); } }
Hello World