java fixedthread_Java newFixedThreadPool方法

Java newFixedThreadPool方法

1 newFixedThreadPool方法的介绍

可以通过调用Executors类的static newFixedThreadPool()方法获得一个固定线程池。

2 newFixedThreadPool方法的语法

ExecutorService fixedPool = Executors.newFixedThreadPool(2);

其中,

最多2个线程将处于活动状态。

如果提交了两个以上的线程,那么它们将保持在队列中,直到线程可用。

如果一个线程由于执行关闭期间的失败而终止,则执行器尚未被调用,则创建一个新线程。

线程会一直存在,直到池关闭。

3 newFixedThreadPool方法的案例

以下TestThread程序显示在线程环境中使用newFixedThreadPool方法。

package com.yiidian;

/**

* 一点教程网: http://www.yiidian.com

*/

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.ThreadPoolExecutor;

import java.util.concurrent.TimeUnit;

public class TestThread {

public static void main(final String[] arguments) throws InterruptedException {

ExecutorService executor = Executors.newFixedThreadPool(2);

// Cast the object to its class type

ThreadPoolExecutor pool = (ThreadPoolExecutor) executor;

//Stats before tasks execution

System.out.println("Core threads: " + pool.getCorePoolSize());

System.out.println("Largest executions: "

+ pool.getLargestPoolSize());

System.out.println("Maximum allowed threads: "

+ pool.getMaximumPoolSize());

System.out.println("Current threads in pool: "

+ pool.getPoolSize());

System.out.println("Currently executing threads: "

+ pool.getActiveCount());

System.out.println("Total number of threads(ever scheduled): "

+ pool.getTaskCount());

executor.submit(new Task());

executor.submit(new Task());

//Stats after tasks execution

System.out.println("Core threads: " + pool.getCorePoolSize());

System.out.println("Largest executions: "

+ pool.getLargestPoolSize());

System.out.println("Maximum allowed threads: "

+ pool.getMaximumPoolSize());

System.out.println("Current threads in pool: "

+ pool.getPoolSize());

System.out.println("Currently executing threads: "

+ pool.getActiveCount());

System.out.println("Total number of threads(ever scheduled): "

+ pool.getTaskCount());

executor.shutdown();

}

static class Task implements Runnable {

public void run() {

try {

Long duration = (long) (Math.random() * 5);

System.out.println("Running Task! Thread Name: " + Thread.currentThread().getName());

TimeUnit.SECONDS.sleep(duration);

System.out.println("Task Completed! Thread Name: "+ Thread.currentThread().getName());

}

catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

输出结果为:

Core threads: 2

Largest executions: 0

Maximum allowed threads: 2

Current threads in pool: 0

Currently executing threads: 0

Total number of threads(ever scheduled): 0

Core threads: 2

Largest executions: 2

Maximum allowed threads: 2

Current threads in pool: 2

Currently executing threads: 1

Total number of threads(ever scheduled): 1

Running Task! Thread Name: pool-1-thread-1

Running Task! Thread Name: pool-1-thread-2

Task Completed! Thread Name: pool-1-thread-1

Task Completed! Thread Name: pool-1-thread-2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值