其他分享
首页 > 其他分享> > 线程池(ThreadPool)创建--jdk1.5

线程池(ThreadPool)创建--jdk1.5

作者:互联网

线程池创建方式jdk1.5
Java通过Executors(jdk1.5并发包)提供四种线程池,分别为:


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程
 * 线程池为无限大,当执行第二个任务时第一个任务已经完成,会复用执行第一个任务的线程,而不用每次新建线程
 * 此线程池不会对线程池大小做限制,线程池大小完全依赖于操作系统(或者说JVM)能够创建的最大线程大小
 */
public class CachedThreadPoolDemo {
    public static void main(String[] args){
        ExecutorService threadPool = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            final int temp = i;
            threadPool.execute(()->{
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+",i"+temp);
            });
        }
    }
}

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待
 */
public class FixedThreadPoolDemo {
    public static void main(String[] args){
        int a = Runtime.getRuntime().availableProcessors();//当前设备的CPU个数
        System.out.println(a);//4
        ExecutorService threadPool = Executors.newFixedThreadPool(a);
        for (int i = 0; i < 10; i++) {
            final int temp = i;
            threadPool.execute(()->{
                //因为线程池大小为a,每个任务输出temp后sleep 2秒,所以每两秒打印a个数字
                System.out.println(Thread.currentThread().getName()+",i:"+temp);
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
    }
}

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 创建一个定长线程池,支持定时及周期性任务执行
 */
public class ScheduledThreadPoolDemo {
    public static void main(String[] args){
        ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(5);
        for (int i = 0; i < 10; i++) {
            final int temp = i;
            threadPool.schedule(() -> {
                System.out.println("i:"+temp);
            },3, TimeUnit.SECONDS); //延迟3秒执行
        }
    }
}

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行
 * 结果依次输出,相当于顺序执行各个任务
 */
public class SingleThreadExecutorDemo {
    public static void main(String[] args){
        ExecutorService threadExecutor = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 10; i++) {
            final int index = i;
            threadExecutor.execute(()->{
                System.out.println("index:"+index);
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
    }
}

标签:util,java,Executors,int,jdk1.5,ThreadPool,线程,import
来源: https://www.cnblogs.com/fly-book/p/11452947.html