8-6多线程----JDK5.0新增线程创建方式(2)使用线程池

JDK5.0新增线程创建方式(2)使用线程池

一、背景:
经常创建和销毁、使用量特别大的资源,比如并发情况下的线程,对性能影响很大。 

二、思路:提前创建好多个线程,放入线程池中,使用时直接获取,使用完放回池中。可以避免频繁创建销毁、实现重复利用。类似生活中的公共交通工具。

三、好处:

  1. 提高响应速度(减少了创建新线程的时间)
  2. 降低资源消耗(重复利用线程池中线程,不需要每次都创建)
  3. 便于线程管理
    (1)corePoolSize:核心池的大小
    (2)maximumPoolSize:最大线程数 
    (3)keepAliveTime:线程没有任务时最多保持多长时间后会终止

四、线程池相关API
1. JDK 5.0起提供了线程池相关API:ExecutorService 和 Executors
2. ExecutorService:真正的线程池接口。常见子类ThreadPoolExecutor
(1) void execute(Runnable command) :执行任务/命令,没有返回值,一般用来执行Runnable
(2) Future submit(Callable task):执行任务,有返回值,一般又来执行Callable
(3) void shutdown() :关闭连接池
3. Executors:工具类、线程池的工厂类,用于创建并返回不同类型的线程池
(1) Executors.newCachedThreadPool():创建一个可根据需要创建新线程的线程池
(2) Executors.newFixedThreadPool(n); 创建一个可重用固定线程数的线程池
(3) Executors.newSingleThreadExecutor() :创建一个只有一个线程的线程池
(4) Executors.newScheduledThreadPool(n):创建一个线程池,它可安排在给定延迟后运行命令或者定期地执行.

代码:

package java2;

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

class NumberThread implements Runnable{
    @Override
    public void run() {
        for(int i = 0;i <= 40;i++){
            if(i % 2 == 0){
                System.out.println(Thread.currentThread().getName() + ": " + i);
            }
        }
    }
}

class NumberThread1 implements Runnable{
    @Override
    public void run() {
        for(int i = 0;i <= 40;i++){
            if(i % 2 != 0){
                System.out.println(Thread.currentThread().getName() + ": " + i);
            }
        }
    }
}

public class ThreadPool {
    public static void main(String[] args) {
        //1. 提供指定线程数量的线程池
        ExecutorService service = Executors.newFixedThreadPool(10);
        ThreadPoolExecutor service1 = (ThreadPoolExecutor) service;
        //设置线程池的属性
//        System.out.println(service.getClass());
//        service1.setCorePoolSize(15);
//        service1.setKeepAliveTime();

        //2.执行指定的线程的操作。需要提供实现Runnable接口或Callable接口实现类的对象
        service.execute(new NumberThread());//适合适用于Runnable
        service.execute(new NumberThread1());//适合适用于Runnable
 
//        service.submit(Callable callable);//适合使用于Callable
        //3.关闭连接池
        service.shutdown();
    }
}

输出:

pool-1-thread-1: 0
pool-1-thread-1: 2
pool-1-thread-1: 4
pool-1-thread-1: 6
pool-1-thread-1: 8
pool-1-thread-1: 10
pool-1-thread-1: 12
pool-1-thread-1: 14
pool-1-thread-1: 16
pool-1-thread-1: 18
pool-1-thread-1: 20
pool-1-thread-2: 1
pool-1-thread-2: 3
pool-1-thread-1: 22
pool-1-thread-2: 5
pool-1-thread-2: 7
pool-1-thread-1: 24
pool-1-thread-2: 9
pool-1-thread-2: 11
pool-1-thread-2: 13
pool-1-thread-2: 15
pool-1-thread-1: 26
pool-1-thread-2: 17
pool-1-thread-2: 19
pool-1-thread-2: 21
pool-1-thread-1: 28
pool-1-thread-2: 23
pool-1-thread-1: 30
pool-1-thread-2: 25
pool-1-thread-1: 32
pool-1-thread-2: 27
pool-1-thread-2: 29
pool-1-thread-2: 31
pool-1-thread-1: 34
pool-1-thread-2: 33
pool-1-thread-2: 35
pool-1-thread-2: 37
pool-1-thread-2: 39
pool-1-thread-1: 36
pool-1-thread-1: 38
pool-1-thread-1: 40
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YY鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值