多线程实现方法之一
2016-07-21 00:24
375 查看
一、创建线程池工厂
**
* 线程池工厂
*/
public class ThreadPoolFactory {
public static ThreadPoolProxy threadPoolProxy;
public static final int Common_CORE_POOL_SIZE=5;
public static final int Common_MAX_POOL_SIZE=5;
public static final int Common_KEEP_LIVE_TIME=1;
// 单例模式
public static ThreadPoolProxy getCommonThreadPool(){
if(threadPoolProxy==null){
synchronized (ThreadPoolFactory.class){
if(threadPoolProxy==null){
threadPoolProxy=new ThreadPoolProxy(Common_CORE_POOL_SIZE,Common_MAX_POOL_SIZE,Common_KEEP_LIVE_TIME);
}
}
}
return threadPoolProxy;
}
}
二、线程池代理
/**
*线程池代理
*/
public class ThreadPoolProxy {
private ThreadPoolExecutor tpx;//线程池执行者
private int corePoolSize;//线程池数
private int maximumPoolSize;//最大最小数
private long keepAliveTime;//活跃时间
public ThreadPoolProxy(int corePoolSize, int maximumPoolSize, long keepAliveTime) {
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.keepAliveTime = keepAliveTime;
}
public ThreadPoolExecutor initThreadPoolExecutor(){
if(tpx == null){
synchronized (ThreadPoolProxy.class){
if (tpx == null){
BlockingQueue<Runnable> workQueue = new LinkedBlockingDeque<>();//基于链表的双端阻塞队列
ThreadFactory threadFactory = Executors.defaultThreadFactory();//默认线程工厂
RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
tpx = new ThreadPoolExecutor(corePoolSize,maximumPoolSize,keepAliveTime, TimeUnit.SECONDS,workQueue,threadFactory,handler);
}
}
}
return tpx;
}
// 执行任务
public void execute(Runnable task) {
initThreadPoolExecutor();
tpx.execute(task);
}
// 移除任务
public void remove(Runnable task) {
initThreadPoolExecutor();
tpx.remove(task);
}
// 提交任务(Future+execute(Runnable task))
public Future<?> summit(Runnable task) {
initThreadPoolExecutor();
return tpx.submit(task);
}
}
三、操作线程
public class ThreadPoolUtils {
// 在非UI线程中执行
public static void runTaskInThread(Runnable task){
ThreadPoolFactory.getCommonThreadPool().execute(task);
}
// 在UI线程中执行
public static Handler handler=new Handler();
public static void runTaskInUIThread(Runnable task){
handler.post(task);
}
}
**
* 线程池工厂
*/
public class ThreadPoolFactory {
public static ThreadPoolProxy threadPoolProxy;
public static final int Common_CORE_POOL_SIZE=5;
public static final int Common_MAX_POOL_SIZE=5;
public static final int Common_KEEP_LIVE_TIME=1;
// 单例模式
public static ThreadPoolProxy getCommonThreadPool(){
if(threadPoolProxy==null){
synchronized (ThreadPoolFactory.class){
if(threadPoolProxy==null){
threadPoolProxy=new ThreadPoolProxy(Common_CORE_POOL_SIZE,Common_MAX_POOL_SIZE,Common_KEEP_LIVE_TIME);
}
}
}
return threadPoolProxy;
}
}
二、线程池代理
/**
*线程池代理
*/
public class ThreadPoolProxy {
private ThreadPoolExecutor tpx;//线程池执行者
private int corePoolSize;//线程池数
private int maximumPoolSize;//最大最小数
private long keepAliveTime;//活跃时间
public ThreadPoolProxy(int corePoolSize, int maximumPoolSize, long keepAliveTime) {
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.keepAliveTime = keepAliveTime;
}
public ThreadPoolExecutor initThreadPoolExecutor(){
if(tpx == null){
synchronized (ThreadPoolProxy.class){
if (tpx == null){
BlockingQueue<Runnable> workQueue = new LinkedBlockingDeque<>();//基于链表的双端阻塞队列
ThreadFactory threadFactory = Executors.defaultThreadFactory();//默认线程工厂
RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
tpx = new ThreadPoolExecutor(corePoolSize,maximumPoolSize,keepAliveTime, TimeUnit.SECONDS,workQueue,threadFactory,handler);
}
}
}
return tpx;
}
// 执行任务
public void execute(Runnable task) {
initThreadPoolExecutor();
tpx.execute(task);
}
// 移除任务
public void remove(Runnable task) {
initThreadPoolExecutor();
tpx.remove(task);
}
// 提交任务(Future+execute(Runnable task))
public Future<?> summit(Runnable task) {
initThreadPoolExecutor();
return tpx.submit(task);
}
}
三、操作线程
public class ThreadPoolUtils {
// 在非UI线程中执行
public static void runTaskInThread(Runnable task){
ThreadPoolFactory.getCommonThreadPool().execute(task);
}
// 在UI线程中执行
public static Handler handler=new Handler();
public static void runTaskInUIThread(Runnable task){
handler.post(task);
}
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories