线程池newFixedThreadPool的使用
来源:互联网 发布:node js 安装 编辑:程序博客网 时间:2024/06/05 20:33
新的线程加入后,如果正在运行的线程达到了上限,则会阻塞,直到有了空闲的线程来运行。
- import java.util.Random;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.TimeUnit;
- /**
- * 线程池newFixedThreadPool的使用。
- *
- * @author 赵学庆,Java世纪网(java2000.net)
- *
- */
- public class ExecutorTest {
- public static void main(String args[]) {
- Random random = new Random();
- // 建立一个容量为3的固定尺寸的线程池
- ExecutorService executor = Executors.newFixedThreadPool(3);
- // 判断可是线程池可以结束
- int waitTime = 500;
- for (int i = 0; i < 10; i++) {
- String name = "线程 " + i;
- int time = random.nextInt(1000);
- waitTime += time;
- Runnable runner = new ExecutorThread(name, time);
- System.out.println("增加: " + name + " / " + time);
- executor.execute(runner);
- }
- try {
- Thread.sleep(waitTime);
- executor.shutdown();
- executor.awaitTermination(waitTime, TimeUnit.MILLISECONDS);
- } catch (InterruptedException ignored) {
- }
- }
- }
- class ExecutorThread implements Runnable {
- private final String name;
- private final int delay;
- public ExecutorThread(String name, int delay) {
- this.name = name;
- this.delay = delay;
- }
- public void run() {
- System.out.println("启动: " + name);
- try {
- Thread.sleep(delay);
- } catch (InterruptedException ignored) {
- }
- System.out.println("完成: " + name);
- }
- }
- 线程池newFixedThreadPool的使用
- 线程池newFixedThreadPool的使用
- 线程池newFixedThreadPool的使用收藏
- Executors.newFixedThreadPool线程池的使用
- 线程池newFixedThreadPool的使用方法
- android 多线程 - 线程池 Executors.newFixedThreadPool 的使用例子
- android 多线程 - 线程池 Executors.newFixedThreadPool 的使用例子
- 线程池的使用 ExecutorService newFixedThreadPool(int nThreads)
- 线程池的使用(newCachedThreadPool、newFixedThreadPool、newScheduledThreadPool、newSingleThreadExecutor)
- newfixedthreadpool线程池 与newCachedThreadPool 的区别
- 线程池Executors.newFixedThreadPool
- 线程池Executors.newFixedThreadPool
- 线程池Executors.newFixedThreadPool
- newFixedThreadPool线程池
- java线程池newFixedThreadPool详解
- newFixedThreadPool线程池数量问题
- Java 自带的线程池Executors.newFixedThreadPool
- Java 自带的线程池Executors.newFixedThreadPool
- SWT做的一个日历控件
- linux ftp限定用户目录
- linux 修改默认语言环境
- 车牌识别源代码
- XFire客户端流程分析
- 线程池newFixedThreadPool的使用
- GSM信道介绍
- Java程序员面试三十二问
- ASP.NET2005 Web.config详解
- Top Ten Oracle JDBC Tips (from: http://www.onjava.com/pub/a/onjava/2001/12/19/oraclejdbc.html)
- 在.net 中 将线性结构数据列表转换为 XML 格式的层次结构
- 联合体的难题
- AIX上安装EBS R12.0.4
- void *的错误(error C2036: 'void *' : unknown size)