Created
November 21, 2015 10:14
-
-
Save jeremyxu2010/c1597ef8820599f1a937 to your computer and use it in GitHub Desktop.
add CachedThreadPool with queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.util.concurrent.*; | |
/** | |
* Created by jeremy on 15/11/20. | |
*/ | |
public class CachedThreadPoolWithQueue { | |
private static final RejectedExecutionHandler defaultHandler = new ThreadPoolExecutor.AbortPolicy(); | |
private final ThreadPoolExecutor executor; | |
private final ThreadPoolExecutor queueExecutor; | |
public CachedThreadPoolWithQueue(int coreThreadSize, int maxThreadSize, int queueSize, long keepAliveTime, | |
TimeUnit unit) { | |
this(coreThreadSize, maxThreadSize, queueSize, keepAliveTime, unit, defaultHandler); | |
} | |
public CachedThreadPoolWithQueue(int coreThreadSize, int maxThreadSize, int queueSize, long keepAliveTime, | |
TimeUnit unit, RejectedExecutionHandler handler) { | |
this.executor = new ThreadPoolExecutor(coreThreadSize, maxThreadSize, keepAliveTime, unit, new SynchronousQueue<Runnable>(), new ThreadPoolExecutor.AbortPolicy()); | |
this.queueExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(queueSize - 1), handler); | |
} | |
public void execute(Runnable job) { | |
queueExecutor.execute(new SubmitJob(executor, job)); | |
} | |
private static class SubmitJob implements Runnable { | |
private final Runnable job; | |
private final ThreadPoolExecutor executor; | |
public SubmitJob(ThreadPoolExecutor executor, Runnable job) { | |
this.executor = executor; | |
this.job = job; | |
} | |
@Override | |
public void run() { | |
while(true){ | |
boolean submitted = this.submitJob(); | |
if(submitted){ | |
break; | |
} else { | |
try { | |
synchronized (this) { | |
this.wait(50L); | |
} | |
Thread.yield(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
break; | |
} | |
} | |
} | |
} | |
private boolean submitJob() { | |
boolean submitted = false; | |
try { | |
this.executor.execute(this.job); | |
submitted = true; | |
} catch (RejectedExecutionException e){ | |
submitted = false; | |
} | |
return submitted; | |
} | |
} | |
public static void main(String[] args) { | |
CachedThreadPoolWithQueue pool = new CachedThreadPoolWithQueue(2, 8, 16, 2L, TimeUnit.SECONDS); | |
try { | |
for (int i = 0; i < 17; i++) { | |
pool.execute(new TestJob(i)); | |
} | |
} catch (Throwable e){ | |
e.printStackTrace(); | |
} | |
try { | |
Thread.sleep(10000000000L); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
private static class TestJob implements Runnable { | |
private final int i; | |
public TestJob(int i) { | |
this.i = i; | |
} | |
@Override | |
public void run() { | |
System.out.println(i); | |
try { | |
Thread.sleep(2000L); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment