Java多线程之线程池的使用示例
2018-01-05 00:00
591 查看
第一种:创建一个定长的线程池,控制线程最大并发数,超出的会在队列中等待。
TestThreadPool.javaimport java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class TestThreadPool { public static void main(String[] args) { long startTime = System.currentTimeMillis(); //获取开始时间 ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);//设置线程池的最大线程数 for (int i = 0; i < 10; i++) { final int index = i;//一般多线程并发都用final fixedThreadPool.execute(new Runnable() { public void run() { System.out.println(index + " " + Thread.currentThread().getName()); } }); } long endTime = System.currentTimeMillis(); //获取结束时间 System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); } }
输出结果:
(当然输出结果不是固定的,不过线程数一定不会超过5个)
可以看到 Thread.currentThread().getName() 拿到的name只有5种,说明最大线程数控制在 5 个
第二种:创建一个可缓存的线程池,可以灵活回收空闲线程,若无可回收,则新建线程。
TestThreadPool1.javaimport java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class TestThreadPool1 { public static void main(String[] args) { ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); for (int i = 0; i < 10000; i++) { final int index = i; cachedThreadPool.execute(new Runnable() { public void run() { System.out.println(index + " " + Thread.currentThread().getName()); } }); } } }
输出结果的 Thread.currentThread().getName() 拿到的name有一两千种(当然不同环境和配置的机器的结果最大线程数是不同的)
第三种:创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序执行。
TestThreadPool2.javaimport java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class TestThreadPool2 { public static void main(String[] args) { ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); for (int i = 0; i < 10000; i++) { final int index = i; singleThreadExecutor.execute(new Runnable() { public void run() { System.out.println(index + " " + Thread.currentThread().getName()); } }); } } }
输出结果:无论循环100次还是100000次,输出结果Thread.currentThread().getName()的值都会是
pool-1-thread-1
第四种:创建一个定长线程池,可以延时或定时周期性地执行任务。
TestThreadPool3.javaimport java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class TestThreadPool3 { public static void main(String[] args) { ExecutorService singleThreadExecutor = Executors.newCachedThreadPool(); for (int i = 0; i < 10; i++) { final int index = i; singleThreadExecutor.execute(new Runnable() { public void run() { try { while(true) { System.out.println(index + " " + Thread.currentThread().getName()); Thread.sleep(10 * 1000); } } catch (InterruptedException e) { e.printStackTrace(); } } }); } } }
输出结果:每隔10s就会输出10行结果
**使用 ScheduledExecutorService 的 scheduleAtFixedRate方法可以设置延时和执行间隔
scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, and so on.
意思是创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在 initialDelay 后开始执行,然后在 initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推。
TestThreadPool4.java
import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class TestThreadPool4 { public static void main(String[] args) { ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5); System.out.println(System.currentTimeMillis()); scheduledThreadPool.scheduleAtFixedRate(new Runnable() { public void run() { System.out.println(System.currentTimeMillis()); } }, 5, 10, TimeUnit.SECONDS); } }
从输出结果可以看出,延时5s后每隔10s会输出一次当前时间。
**使用ScheduledExecutorService的schedule可以设置首次执行延时
schedule(Runnable command, long delay, TimeUnit unit)
Creates and executes a one-shot action that becomes enabled after the given delay.
创建并执行在给定延迟后启用的一次性操作。
TestThreadPool5.java
import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class TestThreadPool5 { public static void main(String[] args) { ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5); for (int i = 0; i < 10; i++) { final int index = i; scheduledThreadPool.schedule(new Runnable() { public void run() { System.out.println(index + " " + Thread.currentThread().getName()); } }, 3, TimeUnit.SECONDS); } } }
输出结果:运行3s后会输出10行结果,而不会每隔3s输出一行。
相关文章推荐
- java多线程详解(7)-线程池的使用
- 我之见--java多线程 线程池使用
- JAVA基础再回首(二十五)——Lock锁的使用、死锁问题、多线程生产者和消费者、线程池、匿名内部类使用多线程、定时器、面试题
- Java多线程研究04-线程池的使用(ThreadPoolExecutor详解)
- 遇到问题----java---多线程----线程数一直增加----web项目中线程池的正确使用方式
- Java的多线程与线程池的使用
- Java_多线程_创建和使用Java自带的线程池
- Java四种线程池的介绍及详细使用代码示例
- Java中四种线程池的使用示例详解
- [Java多线程]-线程池的基本使用和部分源码解析(创建,执行原理)
- Java多线程(十二):线程池的使用
- Java:多线程,线程池,使用CompletionService通过Future来处理Callable的返回结果
- Java多线程总结(三):Java四种常用线程池的使用和分析
- Java多线程查找指定文件夹下包含指定关键字的文件数量(未使用线程池版)
- Java多线程编程-线程池的使用
- java多线程系列(六)---线程池原理及其使用
- java多线程-专题-聊聊并发(三)Java线程池的分析和使用
- (四) Java多线程详解之四种不同线程池使用详解
- Java:多线程,线程池,使用CompletionService通过Future来处理Callable的返回结果
- java 使用线程池做DDOS 攻击示例