Home  >  Core Java

Example of ThreadPoolExecutor.AbortPolicy in Java

By Arvind Rai, January 07, 2013
1. ThreadPoolExecutor.AbortPolicy belongs to java.util.concurrent package. It has been introduced in JDK 1.5.
2. ThreadPoolExecutor.AbortPolicy is the handler for rejected task. It always throws RejectedExecutionException.
3. ThreadPoolExecutor.AbortPolicy 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()); 
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
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us