线程池

线程池:

 

image

 

image

 

4中线程池的创建和简单使用:

 

  1 import java.util.concurrent.ExecutorService;
  2 import java.util.concurrent.Executors;
  3 import java.util.concurrent.ScheduledExecutorService;
  4 
  5 public class DemoClass4ThreadPool {
  6     public static void main(String[] args) {
  7         //TODO 线程池 Pool
  8         //定义:所谓的线程池,其实就是线程对象的容器
  9         //可以根据需要,在启动时,创建一个或者多个线程对象
 10 
 11         //Java种有4中比较常见的线程池
 12         /*
 13         * 1. 创建固定的线程池对象
 14         * */
 15        /* ExecutorService executorService = Executors.newFixedThreadPool(3);
 16         for (int i = 0; i < 5; i++) {
 17             executorService.submit(new Runnable() {
 18                 @Override
 19                 public void run() {
 20                     System.out.println(Thread.currentThread().getName());
 21                 }
 22             });
 23         }
 24         *//*
 25         * 输出结果:
 26             pool-1-thread-2
 27             pool-1-thread-1
 28             pool-1-thread-3
 29             pool-1-thread-1
 30             pool-1-thread-2
 31         * */
 32 
 33         /*
 34         * 2.根据需求动态创建线程池对象的
 35         * */
 36        /* ExecutorService executorService2 = Executors.newCachedThreadPool();
 37         for (int i = 0; i < 10; i++) {
 38             executorService2.submit(new Runnable() {
 39                 @Override
 40                 public void run() {
 41                     System.out.println(Thread.currentThread().getName());
 42                 }
 43             });
 44         }
 45         *//*
 46         * 输出结果: 如果for情况自定线程池里的线程对象,当然也可根据情况重复利用:例如结果里的:2 线程,用了2次
 47         pool-1-thread-1
 48         pool-1-thread-4
 49         pool-1-thread-6
 50         pool-1-thread-3
 51         pool-1-thread-2
 52         pool-1-thread-7
 53         pool-1-thread-5
 54         pool-1-thread-8
 55         pool-1-thread-9
 56         pool-1-thread-2
 57         * */
 58 
 59         /*
 60         * 3. 单一线程,某个工作按照顺序来执行,可以用此
 61         * */
 62        /* ExecutorService executorService3 = Executors.newSingleThreadExecutor();
 63         for (int i = 0; i < 10; i++) {
 64             executorService3.submit(new Runnable() {
 65                 @Override
 66                 public void run() {
 67                     System.out.println(Thread.currentThread().getName());
 68                 }
 69             });
 70         }
 71         *//*
 72         * 输出结果:
 73         pool-1-thread-1
 74         pool-1-thread-1
 75         pool-1-thread-1
 76         pool-1-thread-1
 77         pool-1-thread-1
 78         pool-1-thread-1
 79         pool-1-thread-1
 80         pool-1-thread-1
 81         pool-1-thread-1
 82         pool-1-thread-1
 83         * */
 84 
 85         /*
 86         * 4.定时调度线程,这里只是说明一下 ,具体使用情况,见后期总结
 87         * */
 88         ScheduledExecutorService scheduledExecutorService4 = Executors.newScheduledThreadPool(3);
 89         for (int i = 0; i < 5; i++) {
 90             scheduledExecutorService4.submit(new Runnable() {
 91                 @Override
 92                 public void run() {
 93                     System.out.println(Thread.currentThread().getName());
 94                 }
 95             });
 96         }
 97 
 98                 /*
 99         * 输出结果:
100         pool-1-thread-2
101         pool-1-thread-3
102         pool-1-thread-1
103         pool-1-thread-3
104         pool-1-thread-2
105         * */
106     }
107 }

 

posted @ 2025-10-23 11:50  字节虫  阅读(17)  评论(0)    收藏  举报