Home  >  Core Java

Example of ThreadPoolExecutor.DiscardOldestPolicy in Java

By Arvind Rai, January 11, 2013
1. ThreadPoolExecutor.DiscardOldestPolicy belongs to java.util.concurrent package. It has been introduced in JDK 1.5.
2. ThreadPoolExecutor.DiscardOldestPolicy is the handler for rejected task because of finite bounds of work queue and maximum limit of ThreadPoolExecutor.
3. ThreadPoolExecutor.DiscardOldestPolicy has a method that is rejectedExecution(Runnable r, ThreadPoolExecutor e) which accepts a thread and ThreadPoolExecutor.

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

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

Output
Hello World 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us