线程池
2016-01-08 14:17
330 查看
简单的调用:
自定义线程池:
public class a { public void A() { // 创建一个线程池 ExecutorService executorService = Executors.newFixedThreadPool(10); executorService.execute(new MyRunnable()); } class MyRunnable implements Runnable { @Override public void run() { // TODO Auto-generated method stub } } }
自定义线程池:
public class ThreadManager { private static ThreadProxyPool threadProxyPool; private static Object object = new Object(); public static ThreadProxyPool getThreadProxyPool() { synchronized (object) { if (threadProxyPool == null) { threadProxyPool = new ThreadProxyPool(10, 10, 5L); } return threadProxyPool; } } public static class ThreadProxyPool { private ThreadPoolExecutor threadPoolExecutor; private int corePoolSize; private int maximumPoolSize; private long keepAliveTime; public ThreadProxyPool(int corePoolSize, int maximumPoolSize, long keepAliveTime) { this.corePoolSize = corePoolSize; this.maximumPoolSize = maximumPoolSize; this.keepAliveTime = keepAliveTime; } public void execute(Runnable runnable) { if (runnable == null) { return; } if (threadPoolExecutor == null || threadPoolExecutor.isShutdown()) { threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), Executors.defaultThreadFactory(), new AbortPolicy()); } threadPoolExecutor.execute(runnable); } public void cancel(Runnable runnable) { if (runnable != null && !threadPoolExecutor.isShutdown()) { threadPoolExecutor.getQueue().remove(runnable); } } } }
相关文章推荐
- spring hibernate structs
- JSONArray的应用
- 国外程序员常去的14个顶级开发社区
- ListView 右侧导航
- 通过编写Shell、Python脚本来实现在线管理KVM虚拟机
- UIWebView设置图片的宽度适应屏幕的大小
- 如何下载谷歌地球高程数据为DEM
- {Unity} 平台预定义
- storm 入门原理介绍
- CGBitmapContextCreate函数
- js判断当前的访问是手机/电脑
- cocos2d-x scoket 解决方案
- Java 密码扩展无限制权限策略文件
- 指针 p238
- 微信图文美化编辑器试试构思编辑器
- 使用HTML CSS实现网页换肤效果(二)
- 秋农家常菜——老干妈蒜香土豆丝and秘制炝炒莴笋叶
- Android 软键盘和emoji表情切换方案,和微信几乎一样的体验
- XMPP介绍二:Core Data
- Xcode描述文件路径