ThreadPoolExecutor.DiscardPolicy in Java

By Arvind Rai, November 15, 2023
1. ThreadPoolExecutor.DiscardPolicy belongs to java.util.concurrent package. It is introduced in Java 5.
2. ThreadPoolExecutor.DiscardPolicy is the handler for rejected task by ThreadPoolExecutor. It silently discards the rejected task and throws no error.
3. ThreadPoolExecutor.DiscardPolicy has a method rejectedExecution(Runnable r, ThreadPoolExecutor e) which accepts a Runnable task and ThreadPoolExecutor.

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

Example

DiscardPolicyDemo.java
package com.concretepage;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class DiscardPolicyDemo {
	   class DemoThread implements Runnable {
		   public void run() {
		    	 System.out.println("Hello World");
	           }
	   }
	   public static void main(String... args) {
		   Runnable th=new DiscardPolicyDemo().new DemoThread();
		     
		   ThreadPoolExecutor  tpool= new ThreadPoolExecutor(2,3,500, TimeUnit.MILLISECONDS,
		    		 new LinkedBlockingQueue<Runnable>(), new ThreadPoolExecutor.DiscardPolicy());
		     
		   tpool.execute(th);
	   }
}
Output
Hello World 
POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us