不说了直接贴代码
一、创建线程池
package com.gdcn.core.threadPool;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor.AbortPolicy;
import java.util.concurrent.TimeUnit;
/**
* 线程池创建
* @author 18925186343@189.cn
* @date 2014-3-20
*/
public class ThreadPool {
//线程池维护线程所允许的空闲时间的单位
private static final TimeUnit UNIT = TimeUnit.SECONDS;
//线程池所使用的缓冲队列,这里队列大小为3
private static final BlockingQueue<Runnable> WORKQUEUE = new ArrayBlockingQueue<Runnable>(ThreadPoolCommon.blockingNumber);
//线程池对拒绝任务的处理策略:AbortPolicy为抛出异常;CallerRunsPolicy为重试添加当前的任务,
//他会自动重复调用execute()方法;DiscardOldestPolicy为抛弃旧的任务,DiscardPolicy为抛弃当前的任务
private static final AbortPolicy HANDLER = new ThreadPoolExecutor.AbortPolicy();
public static ThreadPoolExecutor threadPool = null;
private ThreadPool(){
}
/**
* 静态 初始化线程池
*/
static{
threadPool = new ThreadPoolExecutor(ThreadPoolCommon.COREPOOLSIZE, ThreadPoolCommon.MAXINUMPOOLSIZE
, ThreadPoolCommon.KEEPALIVETIME, UNIT, WORKQUEUE, HANDLER);
}
public static void addThreadToPool(Runnable task){
ThreadFactory tf = threadPool.getThreadFactory();
tf.newThread(task);
threadPool.execute(task);
}
}
二、创建线程任务
package com.gdcn.core.threadPool;
import java.io.Serializable;
/**
* 线程任务
* @author 18925186343@189.cn
* @date 2014-1-3
*/
public class ThreadTask implements Runnable,Serializable{
/**
* @author 18925186343@189.cn
* @date 2013-12-19
*/
private static final long serialVersionUID = 6881319121604875915L;
private Object threadPoolTaskData;
public ThreadTask(Object threadPoolTaskData) {
super();
this.threadPoolTaskData = threadPoolTaskData;
}
@Override
public void run() {
System.out.println("线程开始执行........");
threadPoolTaskData = null;
}
public Object getTask(){
return this.threadPoolTaskData;
}
}
三、调用线程池例子
package com.gdcn.core.threadPool;
/**
* 推荐使用匿名内部类 也就是此测试方法中的 方法二和方法四
* @author 18925186343@189.cn
* @date 2014-1-3
*/
public class Test {
public static void main(String[] args) {
/**
* 一种是用方法
*/
ThreadTask test1 = new ThreadTask("Test1"){
/**
* @author 18925186343@189.cn
* @date 2014-1-3
*/
private static final long serialVersionUID = -1363364461049541227L;
@Override
public void run() {
System.out.println("线程Test1在执行.....");
try {
Thread.sleep(1000000000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
ThreadPool.addThreadToPool(test1);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
/**
* 第二种
*/
ThreadPool.addThreadToPool(new Runnable() {
@Override
public void run() {
System.out.println("线程Test2在执行.....");
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
/**
* 第三种
*/
Thread thread1 = new Thread(){
@Override
public void run() {
System.out.println("线程Test3在执行.....");
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
ThreadPool.addThreadToPool(thread1);
/**
* 第四种
*/
ThreadPool.addThreadToPool(new Thread(){
@Override
public void run() {
System.out.println("线程Test4在执行.....");
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
推荐一篇别人的文章,关于线程池的详细解说:
http://www.oschina.net/question/565065_86540
Java底层自带的线程池
最新推荐文章于 2024-09-18 07:30:00 发布