创建多线程的方式四:使用线程池(更多内容看JUC)

2. 创建多线程的方式四:使用线程池
此方式的好处:
> 提高了程序执行的效率。(因为线程已经提前创建好了)
> 提高了资源的复用率。(因为执行完的线程并未销毁,而是可以继续执行其他的任务)
> 可以设置相关的参数,对线程池中的线程的使用进行管理

package chapter10_thread_teacher.src.com.atguigu06.createmore.pool;


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 <= 100;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 <= 100;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());//ThreadPoolExecutor
        service1.setMaximumPoolSize(50); //设置线程池中线程数的上限


        //2.执行指定的线程的操作。需要提供实现Runnable接口或Callable接口实现类的对象
        service.execute(new NumberThread());//适合适用于Runnable
        service.execute(new NumberThread1());//适合适用于Runnable

//        service.submit(Callable callable);//适合使用于Callable
        //3.关闭连接池
        service.shutdown();
    }

}

pool-1-thread-2: 1
pool-1-thread-1: 0
pool-1-thread-1: 2
pool-1-thread-1: 4
pool-1-thread-1: 6
pool-1-thread-2: 3
pool-1-thread-1: 8
pool-1-thread-2: 5
pool-1-thread-1: 10
pool-1-thread-2: 7
pool-1-thread-1: 12
pool-1-thread-2: 9
pool-1-thread-1: 14
pool-1-thread-2: 11
pool-1-thread-1: 16
pool-1-thread-2: 13
pool-1-thread-1: 18
pool-1-thread-2: 15
pool-1-thread-2: 17
pool-1-thread-1: 20
pool-1-thread-1: 22
pool-1-thread-2: 19
pool-1-thread-2: 21
pool-1-thread-1: 24
pool-1-thread-2: 23
pool-1-thread-1: 26
pool-1-thread-2: 25
pool-1-thread-1: 28
pool-1-thread-1: 30
pool-1-thread-2: 27
pool-1-thread-1: 32
pool-1-thread-1: 34
pool-1-thread-2: 29
pool-1-thread-1: 36
pool-1-thread-2: 31
pool-1-thread-1: 38
pool-1-thread-2: 33
pool-1-thread-1: 40
pool-1-thread-1: 42
pool-1-thread-2: 35
pool-1-thread-2: 37
pool-1-thread-2: 39
pool-1-thread-2: 41
pool-1-thread-2: 43
pool-1-thread-2: 45
pool-1-thread-2: 47
pool-1-thread-2: 49
pool-1-thread-2: 51
pool-1-thread-2: 53
pool-1-thread-2: 55
pool-1-thread-2: 57
pool-1-thread-2: 59
pool-1-thread-2: 61
pool-1-thread-1: 44
pool-1-thread-1: 46
pool-1-thread-1: 48
pool-1-thread-1: 50
pool-1-thread-1: 52
pool-1-thread-1: 54
pool-1-thread-1: 56
pool-1-thread-1: 58
pool-1-thread-1: 60
pool-1-thread-2: 63
pool-1-thread-2: 65
pool-1-thread-1: 62
pool-1-thread-2: 67
pool-1-thread-1: 64
pool-1-thread-2: 69
pool-1-thread-1: 66
pool-1-thread-2: 71
pool-1-thread-1: 68
pool-1-thread-2: 73
pool-1-thread-1: 70
pool-1-thread-2: 75
pool-1-thread-1: 72
pool-1-thread-2: 77
pool-1-thread-1: 74
pool-1-thread-1: 76
pool-1-thread-2: 79
pool-1-thread-1: 78
pool-1-thread-2: 81
pool-1-thread-1: 80
pool-1-thread-2: 83
pool-1-thread-1: 82
pool-1-thread-2: 85
pool-1-thread-1: 84
pool-1-thread-2: 87
pool-1-thread-1: 86
pool-1-thread-2: 89
pool-1-thread-1: 88
pool-1-thread-2: 91
pool-1-thread-1: 90
pool-1-thread-2: 93
pool-1-thread-1: 92
pool-1-thread-2: 95
pool-1-thread-1: 94
pool-1-thread-2: 97
pool-1-thread-1: 96
pool-1-thread-2: 99
pool-1-thread-1: 98
pool-1-thread-1: 100


 

 

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值