java 通过Executors.newFixedThreadPool创建固定大小的线程执行器

当我们通过Executors的newCachedThreadPool方法来创建线程执行器的时候,会遇到线程池的线程数量问题。如果线程池中没有空闲的线程,执行器会自动的创建一个新的线程。如果有大量的任务到达,系统会造成超负荷。这个时候可以通过创建固定大小的线程执行器来解决这个问题。当达到最大线程数时,将会将新到达的任务阻塞,直到有空闲的线程。
首先创建一个Server类。用于创建执行器。

package com.java.thread.exccutor;

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

public class Servier2 {
    private ThreadPoolExecutor threadPoolExcutor;

    public Servier2() {
        // 通过Excutor类的newFixedThreadPool()方法来创建一个固定大小的线程执行器
        threadPoolExcutor = (ThreadPoolExecutor) Executors
                .newFixedThreadPool(5);
        // 打印获取已发送到线程执行器上的任务数。
        System.out.printf("Server2 :task count :%d\n",
                threadPoolExcutor.getTaskCount());
    }

    public void executeTask(Task task) {
        System.out.print("Server:A new Task  has Arrived\n ");
        threadPoolExcutor.execute(task);
        System.out.printf("Server:executor's pool Size %d\n",
                threadPoolExcutor.getPoolSize());
        System.out
                .printf("Server:Active count:%d\n", threadPoolExcutor.getActiveCount());
        System.out.printf("Server:Completed Task:%d\n,",
                threadPoolExcutor.getCompletedTaskCount());
    }

    public void endServer() {
        threadPoolExcutor.shutdown();
    }
}

创建一个任务执行类.用于执行任务。

package com.java.thread.exccutor;

import java.util.Date;
import java.util.concurrent.TimeUnit;

public class Task implements Runnable{
    private Date initDate;//声明私有化变量,存储任务创建的时间
    private String name;//任务名称

    public Task(String name){
        initDate = new Date();
        this.name = name;
    }
    public void run() {
        //获取当前任务创建的时间
        System.out.printf("%s:Task %s:Created on :%s \n", Thread.currentThread().getName(),name,initDate);
        //获取当前任务开始时间
        System.out.printf("%s:Task %s:Started on:%s\n",Thread.currentThread().getName(),name,new Date());
        Long duration =(long)(Math.random()*10);
        try {
            System.out.printf("%s:Task %s:Doing a tasking during %d seconds\n",
                    Thread.currentThread().getName(),name,duration);
            TimeUnit.SECONDS.sleep(duration);//线程休眠一段时间
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Main:

package com.java.thread.exccutor;

public class Main2 {

    public static void main(String[] args) {
        Servier2 servier = new Servier2();
        for(int i=0;i<5;i++){
            Task task = new Task("Task"+i);
            servier.executeTask(task);
        }
        servier.endServer();
    }

}
执行结果

Server2 :task count :0
Server:A new Task has Arrived
pool-1-thread-1:Task Task0:Created on :Sun Jun 26 14:59:31 CST 2016
Server:executor’s pool Size 1
pool-1-thread-1:Task Task0:Started on:Sun Jun 26 14:59:31 CST 2016
Server:Active count:1
Server:Completed Task:0
,Server:A new Task has Arrived
pool-1-thread-1:Task Task0:Doing a tasking during 5 seconds
Server:executor’s pool Size 2
Server:Active count:1
Server:Completed Task:0
,Server:A new Task has Arrived
pool-1-thread-2:Task Task1:Created on :Sun Jun 26 14:59:31 CST 2016
pool-1-thread-3:Task Task2:Created on :Sun Jun 26 14:59:31 CST 2016
pool-1-thread-3:Task Task2:Started on:Sun Jun 26 14:59:31 CST 2016
pool-1-thread-3:Task Task2:Doing a tasking during 8 seconds
Server:executor’s pool Size 3
pool-1-thread-2:Task Task1:Started on:Sun Jun 26 14:59:31 CST 2016
pool-1-thread-2:Task Task1:Doing a tasking during 7 seconds
Server:Active count:3
Server:Completed Task:0
,Server:A new Task has Arrived
Server:executor’s pool Size 4
Server:Active count:4
Server:Completed Task:0
,Server:A new Task has Arrived
pool-1-thread-4:Task Task3:Created on :Sun Jun 26 14:59:31 CST 2016
pool-1-thread-4:Task Task3:Started on:Sun Jun 26 14:59:31 CST 2016
pool-1-thread-4:Task Task3:Doing a tasking during 1 seconds
pool-1-thread-5:Task Task4:Created on :Sun Jun 26 14:59:31 CST 2016
pool-1-thread-5:Task Task4:Started on:Sun Jun 26 14:59:31 CST 2016
pool-1-thread-5:Task Task4:Doing a tasking during 7 seconds
Server:executor’s pool Size 5
Server:Active count:5
Server:Completed Task:0
“`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值