关于java 线程池 ThreadPoolExceutor 之 TestDemo

public class App {
public static void main(String[] args) throws InterruptedException {
System.out.println("这是测试项目");

// ExecutorService executorService=Executors.newCachedThreadPool();
// Executors.newSingleThreadExecutor();
// Executors.newFixedThreadPool(100);
ExecutorService executorService = Executors.newFixedThreadPool(5);

// ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 200,
// TimeUnit.MICROSECONDS,
// new ArrayBlockingQueue<Runnable>(5));

while (true) {
for (int i = 0; i < 10; i++) {
Mytask mytask = new Mytask(i);
//executor.execute(mytask);
executorService.execute(mytask);

// System.out.println("线程池线程数目"+executor.getPoolSize()+"
// 等待的任务数目"+executor.getQueue().size()
// +",已执行完别的任务数目:"+executor.getCompletedTaskCount());

System.out.println("当前线程活动数目:" + ((ThreadPoolExecutor) executorService).getActiveCount() + ",等待的任务数目:"
+ ((ThreadPoolExecutor) executorService).getTaskCount() + ",已执行完别的任务数目"
+ ((ThreadPoolExecutor) executorService).getCompletedTaskCount());
}
//executorService.shutdown();
//executor.shutdown();
System.out.println("主线程睡眠10秒 再执行~~~~~~~~~~~~~~~");
Thread.sleep(10000);
}
}
}

-----------------------------------------------------------

public class Mytask implements Runnable {
private int taskNum;

public Mytask(int num) {
this.taskNum = num;
}

@Override
public void run() {
System.out.println("正在执行task" + this.taskNum);
try {

Thread.currentThread();
Thread.sleep(4000);

} catch (InterruptedException e) {

e.printStackTrace();
}
System.out.println("task" + taskNum + "执行完毕");

}

}

--------------------注解
 
得到 ExecutorService 当前活动的线程数
 
https://blog.csdn.net/yingzishizhe/article/details/8769907
 
Java并发编程:线程池的使用(重点需要看)
 
http://www.cnblogs.com/dolphin0520/p/3932921.html
 
Java通过Executors提供四种线程池
 
https://www.cnblogs.com/webglcn/p/5265901.html
 
Executors创建的4种线程池的使用
 
https://www.cnblogs.com/ljp-sun/p/6580147.html

 

posted on 2018-07-06 17:55  chengjunde  阅读(182)  评论(0编辑  收藏  举报

导航