ThreadPoolExecutor.AbortPolicy in Java

By Arvind Rai, November 15, 2023
1. ThreadPoolExecutor.AbortPolicy belongs to java.util.concurrent package. It is been introduced in Java 5.
2. ThreadPoolExecutor.AbortPolicy is the handler for rejected task. It always throws RejectedExecutionException.
3. It is the default policy while instantiating ThreadPoolExecutor.

ThreadPoolExecutor.AbortPolicy is passed as an argument while initializing ThreadPoolExecutor.
ThreadPoolExecutor  tpool= new ThreadPoolExecutor(2,3,500, TimeUnit.MILLISECONDS,
	new LinkedBlockingQueue<Runnable>(), new ThreadPoolExecutor.AbortPolicy()); 

Example

ThreadPoolExecutor.AbortPolicy is not required to pass as an argument because it is default policy. Find the sample example.
AbortPolicyDemo.java
package com.concretepage;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class AbortPolicyDemo {
	   class DemoThread implements Runnable {
		   public void run() {
		    	 System.out.println("Hello World");
	           }
	   }
	   public static void main(String... args) {
		   Runnable th=new AbortPolicyDemo().new DemoThread();
		     
		   ThreadPoolExecutor  tpool= new ThreadPoolExecutor(2,3,500, TimeUnit.MILLISECONDS,
		    		 new LinkedBlockingQueue<Runnable>());
		     
		   tpool.execute(th);
	   }
} 
Output
Hello World 
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us