本文共 15266 字,大约阅读时间需要 50 分钟。
关于线程的基础知识,比如线程的创建(Thread,Runnable),进程和线程的区别,以及线程的sleep、synchronized、wait、interrupt、join、yield等方法就不详细讲解了。有需要的可以参考海子大神的文章。
创建线程池
在java doc中,并不提倡我们直接使用ThreadPoolExecutor,而是使用Executors类中提供的几个静态方法来创建线程池:
1 2 3 | Executors.newCachedThreadPool(); //创建一个缓冲池,缓冲池容量大小为Integer.MAX_VALUE Executors.newSingleThreadExecutor(); //创建容量为1的缓冲池 Executors.newFixedThreadPool(int); //创建固定容量大小的缓冲池 |
线程池的使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | package map; import java.util.Date; import java.util.concurrent.*; /** * Created by benjamin on 12/24/15. */ public class ExecutorsUse { public static void main(String[] args) throws InterruptedException { fixedThreadPool(); singleThreadPool(); newCachedThreadPool(); scheduledThreadPool(); singleThreadScheduledPool(); customThreadPool(); } /** * 固定大小的线程池 */ private static void fixedThreadPool() throws InterruptedException { // 获取线程池最优大小 int fixNum = Runtime.getRuntime().availableProcessors() + 1; ExecutorService fixedThreadPool = Executors.newFixedThreadPool(fixNum); Thread thread1 = new FixedThread(); Thread thread2 = new FixedThread(); Thread thread3 = new FixedThread(); Thread thread4 = new FixedThread(); Thread thread5 = new FixedThread(); fixedThreadPool.execute(thread1); fixedThreadPool.execute(thread2); fixedThreadPool.execute(thread3); fixedThreadPool.execute(thread4); fixedThreadPool.execute(thread5); // 关闭线程池,不让其他线程加入,但是不终止线程的运行 fixedThreadPool.shutdown(); // 会等待线程池的线程都执行结束,才执行下面的语句 fixedThreadPool.awaitTermination(5, TimeUnit.MINUTES); System.out.println("线程执行结束..."); /** * 执行结果: * pool-1-thread-1正在执行第: 0次 * pool-1-thread-4正在执行第: 0次 * pool-1-thread-3正在执行第: 0次 * pool-1-thread-5正在执行第: 0次 * pool-1-thread-2正在执行第: 0次 * pool-1-thread-5正在执行第: 1次 * pool-1-thread-3正在执行第: 1次 * pool-1-thread-3正在执行第: 2次 * pool-1-thread-4正在执行第: 1次 * pool-1-thread-4正在执行第: 2次 * pool-1-thread-1正在执行第: 1次 * pool-1-thread-5正在执行第: 2次 * pool-1-thread-2正在执行第: 1次 * pool-1-thread-2正在执行第: 2次 * pool-1-thread-1正在执行第: 2次 * 线程执行结束... */ } /** * 单任务线程池 */ private static void singleThreadPool() throws InterruptedException { ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); Thread thread1 = new FixedThread(); Thread thread2 = new FixedThread(); Thread thread3 = new FixedThread(); Thread thread4 = new FixedThread(); Thread thread5 = new FixedThread(); singleThreadExecutor.execute(thread1); singleThreadExecutor.execute(thread2); singleThreadExecutor.execute(thread3); singleThreadExecutor.execute(thread4); singleThreadExecutor.execute(thread5); singleThreadExecutor.shutdown(); // 关闭线程池,不让其他线程加入,但是不终止线程的运行 singleThreadExecutor.shutdown(); // 会等待线程池的线程都执行结束,才执行下面的语句 singleThreadExecutor.awaitTermination(5, TimeUnit.MINUTES); System.out.println("线程执行结束..."); /** * 执行结果: * pool-1-thread-1正在执行第: 0次 * pool-1-thread-1正在执行第: 1次 * pool-1-thread-1正在执行第: 2次 * pool-1-thread-1正在执行第: 0次 * pool-1-thread-1正在执行第: 1次 * pool-1-thread-1正在执行第: 2次 * pool-1-thread-1正在执行第: 0次 * pool-1-thread-1正在执行第: 1次 * pool-1-thread-1正在执行第: 2次 * pool-1-thread-1正在执行第: 0次 * pool-1-thread-1正在执行第: 1次 * pool-1-thread-1正在执行第: 2次 * pool-1-thread-1正在执行第: 0次 * pool-1-thread-1正在执行第: 1次 * pool-1-thread-1正在执行第: 2次 * 线程执行结束... */ } /** * 可变尺寸的线程池 */ private static void newCachedThreadPool() throws InterruptedException { ExecutorService newCachedThreadPool = Executors.newCachedThreadPool(); Thread thread1 = new FixedThread(); Thread thread2 = new FixedThread(); Thread thread3 = new FixedThread(); Thread thread4 = new FixedThread(); Thread thread5 = new FixedThread(); newCachedThreadPool.execute(thread1); newCachedThreadPool.execute(thread2); newCachedThreadPool.execute(thread3); newCachedThreadPool.execute(thread4); newCachedThreadPool.execute(thread5); newCachedThreadPool.shutdown(); // 关闭线程池,不让其他线程加入,但是不终止线程的运行 newCachedThreadPool.shutdown(); // 会等待线程池的线程都执行结束,才执行下面的语句 newCachedThreadPool.awaitTermination(5, TimeUnit.MINUTES); System.out.println("线程执行结束..."); /** * pool-1-thread-1正在执行第: 0次 * pool-1-thread-5正在执行第: 0次 * pool-1-thread-4正在执行第: 0次 * pool-1-thread-3正在执行第: 0次 * pool-1-thread-2正在执行第: 0次 * pool-1-thread-2正在执行第: 1次 * pool-1-thread-3正在执行第: 1次 * pool-1-thread-4正在执行第: 1次 * pool-1-thread-4正在执行第: 2次 * pool-1-thread-5正在执行第: 1次 * pool-1-thread-5正在执行第: 2次 * pool-1-thread-1正在执行第: 1次 * pool-1-thread-3正在执行第: 2次 * pool-1-thread-2正在执行第: 2次 * pool-1-thread-1正在执行第: 2次 * 线程执行结束... */ } /** * 延迟连接池 * @throws InterruptedException */ private static void scheduledThreadPool() throws InterruptedException { //创建一个线程池,它可安排在给定延迟后运行命令或者定期地执行。 ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5); Thread thread1 = new FixedThread(); Thread thread2 = new FixedThread(); Thread thread3 = new FixedThread(); Thread thread4 = new FixedThread(); Thread thread5 = new FixedThread(); scheduledExecutorService.schedule(thread1, 5, TimeUnit.SECONDS); scheduledExecutorService.schedule(thread2, 5, TimeUnit.SECONDS); scheduledExecutorService.schedule(thread3, 5, TimeUnit.SECONDS); scheduledExecutorService.schedule(thread4, 5, TimeUnit.SECONDS); scheduledExecutorService.schedule(thread5, 5, TimeUnit.SECONDS); System.out.println("现在开始的时间是: " + new Date()); // 关闭线程池,不让其他线程加入,但是不终止线程的运行 scheduledExecutorService.shutdown(); // 会等待线程池的线程都执行结束,才执行下面的语句 scheduledExecutorService.awaitTermination(5, TimeUnit.MINUTES); System.out.println("线程执行结束..."); /** * 现在开始的时间是: Thu Dec 24 18:05:44 CST 2015 * 现在运行的时间是: Thu Dec 24 18:05:49 CST 2015_____ * 现在运行的时间是: Thu Dec 24 18:05:49 CST 2015_____ * pool-1-thread-4正在执行第: 0次 * 现在运行的时间是: Thu Dec 24 18:05:49 CST 2015_____ * pool-1-thread-1正在执行第: 0次 * 现在运行的时间是: Thu Dec 24 18:05:49 CST 2015_____ * pool-1-thread-2正在执行第: 0次 * 现在运行的时间是: Thu Dec 24 18:05:49 CST 2015_____ * 现在运行的时间是: Thu Dec 24 18:05:49 CST 2015_____ * pool-1-thread-2正在执行第: 1次 * 现在运行的时间是: Thu Dec 24 18:05:49 CST 2015_____ * pool-1-thread-1正在执行第: 1次 * 现在运行的时间是: Thu Dec 24 18:05:49 CST 2015_____ * pool-1-thread-3正在执行第: 0次 * pool-1-thread-4正在执行第: 1次 * 现在运行的时间是: Thu Dec 24 18:05:49 CST 2015_____ * pool-1-thread-1正在执行第: 2次 * 现在运行的时间是: Thu Dec 24 18:05:49 CST 2015_____ * pool-1-thread-2正在执行第: 2次 * pool-1-thread-5正在执行第: 0次 * 现在运行的时间是: Thu Dec 24 18:05:49 CST 2015_____ * pool-1-thread-4正在执行第: 2次 * 现在运行的时间是: Thu Dec 24 18:05:49 CST 2015_____ * pool-1-thread-5正在执行第: 1次 * 现在运行的时间是: Thu Dec 24 18:05:49 CST 2015_____ * 现在运行的时间是: Thu Dec 24 18:05:49 CST 2015_____ * pool-1-thread-5正在执行第: 2次 * pool-1-thread-3正在执行第: 1次 * 现在运行的时间是: Thu Dec 24 18:05:49 CST 2015_____ * pool-1-thread-3正在执行第: 2次 * 线程执行结束... */ } /** * 单任务延迟连接池 */ private static void singleThreadScheduledPool() { ScheduledExecutorService pool = Executors.newSingleThreadScheduledExecutor(); /** * 其他都一样,这里就不写了 */ } /** * 自定义连接池 * 自定义连接池稍微麻烦些,不过通过创建的ThreadPoolExecutor线程池对象, * 可以获取到当前线程池的尺寸、正在执行任务的线程数、工作队列等等。 */ private static void customThreadPool() { // 创建等待队列 BlockingQueue bqueue = new ArrayBlockingQueue(20); // 创建一个单线程执行程序,它可安排在给定延迟后运行命令或者定期地执行。 ThreadPoolExecutor pool = new ThreadPoolExecutor(2, 3, 2, TimeUnit.MILLISECONDS, bqueue); // ThreadPoolExecutor参数解释 // corePoolSize - 池中所保存的线程数,包括空闲线程。 // maximumPoolSize - 池中允许的最大线程数。 // keepAliveTime - 当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间。 // unit - keepAliveTime 参数的时间单位。 // workQueue - 执行前用于保持任务的队列。此队列仅保持由 execute 方法提交的 Runnable 任务。 /** * 其他都一样,这里就不写了 */ } } class FixedThread extends Thread { @Override public void run() { for (int i = 0; i < 3; i ++) { System.out.println("现在运行的时间是: " + new Date() + "_____"); System.out.println(Thread.currentThread().getName() + "正在执行第: " + i + "次"); } } } |
在工作中如何设置线程池的大小呢?
设置线程池的大小
线程池的理想大小取决于被提交任务的类型以及所部署系统的特性。在代码中通常不会固定线程池的大小,而应该通过某种配置机制来提供,或者根据Runtime.availableProcessors来动态计算。
幸运的是,要设置线程池的大小也并不困难,只需要避免“过大”和“过小”这两种极端情况。如果线程池过大,那么大量的线程将在相对很少的CPU和内存资源上发生竞争,这不仅会导致更高的内存使用量,而且还可能耗尽资源。如果线程池过小,那么将导致许多空间的处理器无法执行工作,从而降低吞吐率。 要想正确地设置线程池的大小,必须分析计算环境、资源预算和任务的特性。在部署的系统中有多少个CPU?多大的内存?任务是计算密集型、I/O密集型还是二者皆可?它们是否需要像JDBC连接这样的稀缺资源?如果需要执行不同类别的任务,并且它们之间的行为相差很大,那么应该考虑使用多个线程池,从而使每个线程池可以根据各自的工作负载来调整。 对于计算密集型的任务,在拥有Ncpu个处理器的系统上,当线程池的大小为 Ncpu+1 时,通常能实现最优的利用率。 线程池的最优大小等于:Nthreads = Ncpu * Ucpu * (1+W/C)
给定下列定义:
Ncpu是CPU的数目,一般可以通过这个公式获取
int N_CPUS = Runtime.getRuntime().availableProcessors()Ucpu:CPU的利用率,范围为 0<=Ucpu<=1
W/C:是等待时间和计算时间的比值
一般需要根据任务的类型来配置线程池大小:
如果是CPU密集型任务,就需要尽量压榨CPU,参考值可以设为 NCPU+1
如果是IO密集型任务,参考值可以设置为2*NCPU
当然,这只是一个参考值,具体的设置还需要根据实际情况进行调整,比如可以先将线程池大小设置为参考值,再观察任务运行情况和系统负载、资源利用率来进行适当调整。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | package map; import java.util.Random; import java.util.concurrent.*; /** * Created by benjamin on 12/25/15. */ public class CallableAndFuture { public static void main(String[] args) throws ExecutionException, InterruptedException { testCallableAndFuture(); testCallableAndFuture2(); testCallableAndFuture3(); testCallableAndFutureTask(); } /** * 一个简单地Callable 和 future的使用简介 * @throws ExecutionException * @throws InterruptedException */ private static void testCallableAndFuture() throws ExecutionException, InterruptedException { Callable |
参考文章:
转载地址:http://lnmfl.baihongyu.com/