java线程池的一些简单功能,后续会更新,代码不多,很好理解

package com.rbac.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit; /**
* 自定义线程池
*
* @author xl,lang
*
*/
public class MyExecutorService {
// 初始化线程
protected List<InitThread> initThreads; // 执行任务列表
protected BlockingQueue<Runnable> taskQueues; // 线程执行状态
protected volatile boolean threadState = true; /*
* // 当前线程的活跃数 public AtomicInteger activeCount; public Lock lock=new
* ReentrantLock(); // 最小活跃数 public int threadMinSize = 0; // 最大线程数 public
* int threadMaxSize = 0; // 初始话线程数 public int threadInitSize = 0;
*/ /**
* 线程初始化方法
*
* @param threadMaxSize
* @param threadInitSize
* @param taskQueueSize
*/
/*
* public MyExecutorService(int threadMaxSize, int threadMinSize, int
* threadInitSize, int taskQueueSize) { this.taskQueues = new
* LinkedBlockingDeque<Runnable>(taskQueueSize); if (threadInitSize > 0 &&
* threadInitSize < threadMaxSize) { this.initThreads = new ArrayList<>();
* for (int i = 0; i < threadInitSize; i++) { InitThread init = new
* InitThread(); init.start(); initThreads.add(init); }
*
* }
*
* }
*/ /**
* 线程初始化方法
*
* @param threadMaxSize
* @param threadInitSize
* @param taskQueueSize
*/
public MyExecutorService(int threadInitSize, int taskQueueSize) {
this.taskQueues = new LinkedBlockingDeque<Runnable>(taskQueueSize);
if (threadInitSize > 0) {
this.initThreads = new ArrayList<>();
for (int i = 0; i < threadInitSize; i++) {
InitThread init = new InitThread();
init.start();
initThreads.add(init);
} } } // 添加任务
public boolean exeute(Runnable task) throws InterruptedException { return this.taskQueues.offer(task, 60, TimeUnit.SECONDS);
} /**
* 初始化线程
*
* @author xl,lang
*
*/
class InitThread extends Thread { @Override
public void run() { while (threadState || taskQueues.size() > 0) {
Runnable runable = taskQueues.poll();
if (null != runable) {
runable.run();
} } } } // 关闭线程池
public void shutdown() {
this.threadState = false;
} public static void main(String[] args) { MyExecutorService es = new MyExecutorService(2, 5);
for (int i = 0; i < 100; i++) {
final int a = i;
try {
es.exeute(new Runnable() { @Override
public void run() {
System.out.println(Thread.currentThread().getName() + "执行" + a); }
});
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
es.shutdown(); }
}

最新文章

  1. UML统一建模编程
  2. java中的负数的问题
  3. c++的类与对象
  4. commons-fileupload实现文件上传下载
  5. [转]通过PowerShell工具跨多台服务器执行SQL脚本
  6. XAMPP启动mysql遇到的问题
  7. 【和我一起学python吧】python入门语法总结
  8. js/jquery/插件表单验证
  9. PS中模式算法
  10. C#导出EXCEL没有网格线的解决方法
  11. pytesseract使用
  12. 【链表】BZOJ1588: [HNOI2002]营业额统计
  13. PHP 针对多用户 实现头像更换
  14. vue父子组件及非父子组件通信
  15. 关于web前端中遇到的html,css小知识点
  16. UVA12113-Overlapping Squares(二进制枚举)
  17. ASP.NET Core MVC四种枚举绑定方式
  18. oracle中查询表是否存在
  19. RSS阅读
  20. Codeforces729D(SummerTrainingDay01-F)

热门文章

  1. JUC常用同步工具类——CountDownLatch,CyclicBarrier,Semaphore
  2. Cisco模拟器的基本使用
  3. [LeetCode] 面试题59 - II. 队列的最大值
  4. Linq的整型或实体类null引发的报错问题
  5. Nacos 数据持久化 mysql8.0
  6. php通过单例模式使一个类只能创建一个对象。
  7. 【摩天大楼平地起】基础篇 09 简述N种查找算法
  8. 快速上手项目远程团队协作--CODING(新手向)
  9. Natas20 Writeup(Session登录,注入参数)
  10. Fiddler1 简单使用