新的线程加入后,如果正在运行的线程达到了上限,则会阻塞,直到有了空闲的线程来运行。
- import java.util.Random;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.TimeUnit;
- public class ExecutorTest {
- public static void main(String args[]) {
- Random random = new Random();
-
- 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 创建一个定长线程池,可控制线程大并发数,超出的线程会在队列中等待。 newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。 newSingleThreadExecutor 创建一个单...
而线程池不允许使用Executors去创建,而要通过ThreadPoolExecutor方式,这一方面是由于jdk中Executor框架虽然提供了如newFixedThreadPool()、newSingleThreadExecutor()、newCachedThreadPool()等创建线程池的方法,...
ThreadPoolManager:Executors.newFixedThreadPool(num * 2);// 创建一个可重用固定线程数的线程池,以共享的无界队列方式来运行这些线程 threadPool: 线程池 创建线程池,销毁线程池,添加新任务
一、线程池 1、什么是线程池 ...2、线程池的使用 public static ExecutorService newFixedThreadPool(int nThreads); 得到一个线程对象,初始化参数是要求的当前线程池中的线程数 public Future submit(Runnab
//Runtime的availableProcessors()方法返回当前系统的CPU的数目 //系统的CPU越多,线程池中工作线程的数目也越多 executorService= Executors.newFixedThreadPool( Runtime.getRuntime()....
线程池demo:四类线程池的详细使用,newCachedThreadPool, newFixedThreadPool, newScheduledThreadPool, newSingleThreadExecutor
java简单线程池的实现,使用jdk的Executors.newFixedThreadPool方法来实现固定大小线程池
1.newFixedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。`` 2.newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列...
前言 这周我投递出了简历,岗位是java后端开发工程师。这周美团面试官给我进行了面试。面试过程中他问了线程池...Executors.newFixedThreadPool(100);//创建固定大小的线程池 Executors.newSingleThreadExecutor();//创
阿里的代码规范中提出,不推荐使用JDK提供的线程池创建方法(newCachedThreadPool、newFixedThreadPool等),因此对ThreadsPoolExecutor方法进行了简单封装,以方便使用。
阿里的代码规范中提出,不推荐使用JDK提供的线程池创建方法(newCachedThreadPool、newFixedThreadPool等),因此对ThreadPoolExecutor方法进行了简单封装,以方便使用。
1. 线程池的使用 public static ExecutorService newFixedThreadPool(int nThreads); 得到一个线程对象,初始化参数是要求的当前线程池中的线程数 public Future submit(Runnable target); 从线程池中获取一个线程...
(传入的对象实现了Runnable接口, 这就解释了: 为什么实现多线程的方式中继承Thread类不能使用线程池了) 执行过程: 首先判断任务是否为空, 为空抛出空指针异常, 否则执行下一个判断: 当前线程数量是否小于核心...
newFixedThreadPool(固定长度的线程池) newCachedThreadPool(不限规模的线程池) newSingleThreadPool(单线程线程池) newScheduledThreadPool(带延迟/定时的固定长度线程池) 具体...
简单易用的多线程发邮件。其中用的callble,里面的发送的邮件是固定的,利用的newFixedThreadPool。你可以自己调整成其他的线程池,原理一样。
mina2 教程一个server 和client通信,支持同时在线,利用newfixedThreadPool线程池
本项目是一个展示多线程下载的项目,使用线程池(Executors.newFixedThreadPool(2))对线程的管理,支持断点续传,第一个版本默认可以两个线程同时下载。
newFixedThreadPool此种线程池如果线程数达到最大值后会怎么办,底层原理。 多线程之间通信的同步问题,synchronized锁的是对象,衍伸出和synchronized相关很多的具体问题,例如同一个类不同方法都有synchronized锁...
服务端是接收心跳包,而客户端是发送心跳包,由于客户端只与一个服务端进行通讯(客户端之间的通讯也是由服务端进行分发的),所以这里只使用了一个大小为2的线程池去处理这两件事(newFixedThreadPool(2)),对应的...