public class TestPool {
public static void main(String[] args) {
ThreadPoolExecutor pool = getDelayThreadPool();
for (int i = 0; i < 5; i++) {
Random ran = new Random(i);
pool.execute(new DelayThreadTask(Long.valueOf(ran.nextInt(10))));
}
pool.shutdown();
}
private static ThreadPoolExecutor getCacheThreadPool() {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 2, 10000, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue(1), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
return threadPoolExecutor;
}
private static ThreadPoolExecutor getNewFixedThreadPool() {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 2, 10000, TimeUnit.MILLISECONDS,
new SynchronousQueue(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
return threadPoolExecutor;
}
private static ThreadPoolExecutor getNoQueuePool() {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 2, 10000, TimeUnit.MILLISECONDS,
new SynchronousQueue(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
return threadPoolExecutor;
}
private static ThreadPoolExecutor getWaitingPool() {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 3, 10000, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
return threadPoolExecutor;
}
private static ThreadPoolExecutor getPriorityPool() {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(0, 1, 10000, TimeUnit.MILLISECONDS,
new PriorityBlockingQueue<>(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
return threadPoolExecutor;
}
private static ThreadPoolExecutor getThreadFactoryPool() {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 3, 10000, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(), new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return new Thread(r, String.format("[线程池-%s]", r.hashCode()));
}
}, new ThreadPoolExecutor.AbortPolicy());
return threadPoolExecutor;
}
private static ThreadPoolExecutor getThreadPoolExecutor() {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(0, 1, 10000, TimeUnit.MILLISECONDS,
new PriorityBlockingQueue<>(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy()) {
@Override
protected void beforeExecute(Thread t, Runnable r) {
System.out.println(String.format("准备执行,第%s优先级线程:", ((ThreadTask) r).getPriority()));
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
System.out.println(String.format("第%s优先级线程执行完成:", ((ThreadTask) r).getPriority()));
}
@Override
protected void terminated() {
System.out.println("线程池退出了");
}
};
return threadPoolExecutor;
}
private static ThreadPoolExecutor getDelayThreadPool(){
DelayQueue delayThreadTasks = new DelayQueue<>();
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(0, 4, 10000, TimeUnit.MILLISECONDS,
delayThreadTasks, Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
return threadPoolExecutor;
}
private static ScheduledThreadPoolExecutor getScheduledThreadPool(){
ScheduledThreadPoolExecutor se=new ScheduledThreadPoolExecutor(2);
return se;
}
static class ThreadTask implements Runnable, Comparable<ThreadTask> {
private int priority;
public ThreadTask(int priority) {
this.priority = priority;
}
public ThreadTask() {
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
@Override
public int compareTo(ThreadTask o) {
return this.priority > o.priority ? -1 : 1;
}
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(String.format("我是线程:%s, priority:%s", Thread.currentThread().getName(), priority));
}
}
static class DelayThreadTask implements Runnable, Delayed {
private Long millisecond;
private Long expTime;
public DelayThreadTask(Long millisecond) {
this.expTime = System.currentTimeMillis()+millisecond*1000;
this.millisecond=millisecond;
}
public Long getExpTime() {
return expTime;
}
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(String.format("我是线程:%s, 延迟时间:%s秒", Thread.currentThread().getName(), millisecond));
}
@Override
public long getDelay(TimeUnit unit) {
return this.expTime - System.currentTimeMillis();
}
@Override
public int compareTo(Delayed o) {
return this.expTime.compareTo(((DelayThreadTask)o).getExpTime());
}
}
}