thread52
来源:互联网 发布:读取json文件中文乱码 编辑:程序博客网 时间:2024/05/21 09:37
package com.neutron.t23;import java.io.IOException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;/** * 线程池 * 1.固定个数的线程池 * 2.缓存线程池,开始线程数0 * 如果需要线程,当前线程池没有,那么创建线程池 * 如果需要线程,线程池中有没有使用的线程,那么使用已经存在的线程 * 如果线程池中线程超过60秒(默认)没有使用,那么该线程停止 * 3.只有1个线程的线程池 * 保证线程执行的先后顺序 * 4.ScheduledPool * 和DelayedQueue类似,定时执行 * 5.WorkStealingPool(任务窃取,都是守护线程) * 每个线程都有要处理的队列中的任务,如果其中的线程完成自己队列中的任务, * 那么它可以去其他线程中获取其他线程的任务去执行 */public class T242WorkStealingPool { /* 4 1000:ForkJoinPool-1-worker-1 1000:ForkJoinPool-1-worker-0 2000:ForkJoinPool-1-worker-2 3000:ForkJoinPool-1-worker-3 2000:ForkJoinPool-1-worker-1 public static ExecutorService newWorkStealingPool() { return new ForkJoinPool (Runtime.getRuntime().availableProcessors(), ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true); } */ public static void main(String[] args) throws IOException { // 根据cpu是几核来开启几个线程 ExecutorService service = Executors.newWorkStealingPool(); // 查看当前计算机是几核 System.out.println(Runtime.getRuntime().availableProcessors()); service.execute(new R(1000)); service.execute(new R(2000)); service.execute(new R(3000)); service.execute(new R(1000)); service.execute(new R(2000)); // WorkStealing是精灵线程(守护线程、后台线程),主线程不阻塞,看不到输出。 // 虚拟机不停止,守护线程不停止 System.in.read(); } static class R implements Runnable { int time; public R(int time) { this.time = time; } @Override public void run() { try { TimeUnit.MILLISECONDS.sleep(time); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(time + ":" + Thread.currentThread().getName()); } }}
阅读全文
0 0
- thread52
- linux下tomcat的开机重启
- thread51
- Java实践(一)---字符串
- ubuntu16.04_64bitjava服务器配置
- bzoj 4276: [ONTAK2015]Bajtman i Okrągły Robin 线段树优化建图+费用流
- thread52
- Qt 读写Excel并显示到tablewidget
- win10 wifi热点常用命令与问题
- thread53
- redis-set
- 随机提取Access/SqlServer数据库中的10条记录的SQL语句
- Android 官网网址
- Shell中的反引号、单引号与双引号的用法(` /''/"")
- Linux通过SQLPLus操作数据库