服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java多线程 自定义线程池详情

Java多线程 自定义线程池详情

2022-03-04 00:46冬日毛毛雨 Java教程

这篇文章主要介绍了Java多线程 自定义线程池,文章主要是学习代码,没有过多解析,需要的朋友可以参考一下文章的具体内容

主要介绍:

  • 1.任务队列
  • 2.拒绝策略(抛出异常、直接丢弃、阻塞、临时队列)
  • 3.init( min )
  • 4.active
  • 5.max
?
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
min<=active<=max
 
package chapter13;
 
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
 
public class SimpleThreadPool {
 
    public final static DiscardPolicy DEFAULT_DISCARD_POLICY = () -> {
        throw new DiscardException("Discard This Task.");
    };
    private final static int DEFAULT_SIZE = 10;
    //设置线程任务最大值
    private final static int DEFAULT_TASK_QUEUE_SIZE = 2000;
    private final static LinkedList<Runnable> TASK_QUEUE = new LinkedList<>();
    private final static String THREAD_PREFIX = "SIMPLE_THREAD_POOL-";
    private final static ThreadGroup GROUP = new ThreadGroup("Pool_Group");
    private final static List<WorkerTask> THREAD_QUEUE = new ArrayList<>();
    private static volatile int seq = 0;
    private final int size;
    //队列大小
    private final int queueSize;
    private final DiscardPolicy discardPolicy;
 
    private volatile boolean destroy = false;
 
    public SimpleThreadPool() {
        this(DEFAULT_SIZE, DEFAULT_TASK_QUEUE_SIZE, DEFAULT_DISCARD_POLICY);
    }
 
    public SimpleThreadPool(int size, int queueSize, DiscardPolicy discardPolicy) {
        this.size = size;
        this.queueSize = queueSize;
        this.discardPolicy = discardPolicy;
        init();
    }
 
 
 
    private void init() {
 
        for (int i = 0; i < size; i++) {
            createWorkTask();
        }
    }
 
    public void submit(Runnable runnable) {
        if(destroy) throw new IllegalStateException("The thread pool already destroy and not allow submit task.");
        synchronized (TASK_QUEUE) {
            if (TASK_QUEUE.size() > queueSize) discardPolicy.discard();
            TASK_QUEUE.addLast(runnable);
            TASK_QUEUE.notifyAll();
        }
    }
 
    private void createWorkTask() {
        WorkerTask task = new WorkerTask(GROUP, THREAD_PREFIX + (seq++));
        task.start();
        THREAD_QUEUE.add(task);
    }
 
    public void shutdown() throws InterruptedException {
        while (!TASK_QUEUE.isEmpty()) {
            Thread.sleep(50);
        }
 
        int initVal = THREAD_QUEUE.size();
        while (initVal > 0) {
            for (WorkerTask task : THREAD_QUEUE) {
                if (task.getTaskState() == TaskState.BLOCKED) {
                    task.interrupt();
                    task.close();
                    initVal--;
                } else {
                    Thread.sleep(10);
                }
            }
        }
        this.destroy = true;
        System.out.println("The thread pool disposed.");
    }
 
    public int getSize() {
        return size;
    }
 
    public int getQueueSize() {
        return queueSize;
    }
 
    public boolean destory() {
        return this.destroy;
    }
 
    private enum TaskState {
        FREE, RUNNING, BLOCKED, DEAD
    }
 
    public interface DiscardPolicy {
 
        void discard() throws DiscardException;
    }
 
    public static class DiscardException extends RuntimeException {
 
        public DiscardException(String message) {
            super(message);
        }
    }
 
    private static class WorkerTask extends Thread {
 
        private volatile TaskState taskState = TaskState.FREE;
 
        public WorkerTask(ThreadGroup group, String name) {
            super(group, name);
        }
 
        public TaskState getTaskState() {
            return this.taskState;
        }
 
        public void run() {
            OUTER:
            while (this.taskState != TaskState.DEAD) {
 
                Runnable runnable;
                synchronized (TASK_QUEUE) {
                    while (TASK_QUEUE.isEmpty()) {
                        try {
                            taskState = TaskState.BLOCKED;
                            TASK_QUEUE.wait();
                        } catch (InterruptedException e) {
                            break OUTER;
                        }
                    }
 
                    runnable = TASK_QUEUE.removeFirst();
                }
 
                if (runnable != null) {
                    taskState = TaskState.RUNNING;
                    runnable.run();
                    taskState = TaskState.FREE;
                }
            }
        }
 
        public void close() {
            this.taskState = TaskState.DEAD;
        }
    }
 
 
 
    public static void main(String[] args) throws InterruptedException {
        SimpleThreadPool threadPool = new SimpleThreadPool();
        for (int i = 0; i < 40; i++) {
 
            int finalI = i;
            threadPool.submit(() -> {
                System.out.println("The runnable " + finalI + " be serviced by " + Thread.currentThread() + " start.");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("The runnable " + finalI + " be serviced by " + Thread.currentThread() + " finished.");
            });
 
            System.out.println("======================");
 
        }
        Thread.sleep(4000);
        threadPool.shutdown();
        threadPool.submit(()->System.out.println("============="));
    }
 
 
}
?
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
======================
======================
The runnable 0 be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] start.
======================
The runnable 2 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable 1 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] start.
The runnable 3 be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] start.
======================
======================
======================
The runnable 5 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable 6 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] start.
======================
======================
The runnable 7 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable 4 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable 8 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] start.
======================
======================
======================
======================
The runnable 9 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] start.
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
The runnable 7 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable 10 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable 6 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] finished.
The runnable 11 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] start.
The runnable 3 be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] finished.
The runnable 12 be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] start.
The runnable 5 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable 0 be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] finished.
The runnable 14 be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] start.
The runnable 2 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable 15 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable 8 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] finished.
The runnable 16 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] start.
The runnable 4 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable 17 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable 9 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] finished.
The runnable 1 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] finished.
The runnable 19 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] start.
The runnable 18 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] start.
The runnable 13 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable 10 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable 14 be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] finished.
The runnable 11 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] finished.
The runnable 22 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] start.
The runnable 12 be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] finished.
The runnable 21 be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] start.
The runnable 20 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable 23 be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] start.
The runnable 15 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable 24 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable 17 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable 18 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] finished.
The runnable 25 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable 13 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable 16 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] finished.
The runnable 19 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] finished.
The runnable 28 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] start.
The runnable 27 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable 26 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] start.
The runnable 29 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] start.
The runnable 22 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] finished.
The runnable 30 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] start.
The runnable 20 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable 31 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable 23 be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] finished.
The runnable 21 be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] finished.
The runnable 32 be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] start.
The runnable 33 be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] start.
The runnable 24 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable 34 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable 25 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable 29 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] finished.
The runnable 27 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable 35 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable 26 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] finished.
The runnable 28 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] finished.
The runnable 39 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] start.
The runnable 38 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] start.
The runnable 37 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable 36 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] start.
The runnable 33 be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] finished.
The runnable 32 be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] finished.
The runnable 31 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable 30 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] finished.
The runnable 39 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] finished.
The runnable 34 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable 38 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] finished.
The runnable 36 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] finished.
The runnable 37 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable 35 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The thread pool disposed.
Exception in thread "main" java.lang.IllegalStateException: The thread pool already destroy and not allow submit task.
    at chapter13.SimpleThreadPool.submit(SimpleThreadPool.java:48)
    at chapter13.SimpleThreadPool.main(SimpleThreadPool.java:176)

线程池本身就是一个线程:

?
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
 
public class SimpleThreadPool extends Thread {
 
    public final static DiscardPolicy DEFAULT_DISCARD_POLICY = () -> {
        throw new DiscardException("Discard This Task.");
    };
    //设置线程任务最大值
    private final static int DEFAULT_TASK_QUEUE_SIZE = 2000;
    /**
     * 任务队列
     */
    private final static LinkedList<Runnable> TASK_QUEUE = new LinkedList<>();
    private final static String THREAD_PREFIX = "SIMPLE_THREAD_POOL-";
    /**
     * 线程组
     */
    private final static ThreadGroup GROUP = new ThreadGroup("Pool_Group");
    private final static List<WorkerTask> THREAD_QUEUE = new ArrayList<>();
    private static volatile int seq = 0;
    //队列大小
    private final int queueSize;
    private final DiscardPolicy discardPolicy;
    private int size;
    private volatile boolean destroy = false;
 
    /**
     * 最小线程数
     */
    private int min;
    /**
     * 最大线程数
     */
    private int max;
    /**
     * 线程活跃数
     */
    private int active;
 
    public SimpleThreadPool() {
        this(4, 8, 12, DEFAULT_TASK_QUEUE_SIZE, DEFAULT_DISCARD_POLICY);
    }
 
    public SimpleThreadPool(int min, int active, int max, int queueSize, DiscardPolicy discardPolicy) {
        this.min = min;
        this.active = active;
        this.max = max;
        this.queueSize = queueSize;
        this.discardPolicy = discardPolicy;
        init();
    }
 
    private void init() {
 
        for (int i = 0; i < this.min; i++) {
            createWorkTask();
        }
        /**
         * 默认线程大小为最小线程数
         */
        this.size = min;
        this.start();
    }
 
    public void submit(Runnable runnable) {
        if (destroy) throw new IllegalStateException("The thread pool already destroy and not allow submit task.");
        synchronized (TASK_QUEUE) {
            if (TASK_QUEUE.size() > queueSize) discardPolicy.discard();
            TASK_QUEUE.addLast(runnable);
            TASK_QUEUE.notifyAll();
        }
    }
 
    private void createWorkTask() {
        WorkerTask task = new WorkerTask(GROUP, THREAD_PREFIX + (seq++));
        task.start();
        THREAD_QUEUE.add(task);
    }
 
 
    public void shutdown() throws InterruptedException {
 
        while (!TASK_QUEUE.isEmpty()) {
            Thread.sleep(50);
        }
        //加锁 对线程进行操作唯一
        synchronized (THREAD_QUEUE) {
            int initVal = THREAD_QUEUE.size();
            while (initVal > 0) {
                for (WorkerTask task : THREAD_QUEUE) {
                    if (task.getTaskState() == TaskState.BLOCKED) {
                        task.interrupt();
                        task.close();
                        initVal--;
                    } else {
                        Thread.sleep(10);
                    }
                }
            }
 
        }
        this.destroy = true;
        System.out.println("The thread pool disposed.");
    }
 
    public int getSize() {
        return size;
    }
 
    public int getQueueSize() {
        return queueSize;
    }
 
    public boolean isDestory() {
        return this.destroy;
    }
 
    public int getMin() {
        return min;
    }
 
    public int getMax() {
        return max;
    }
 
    public int getActive() {
        return active;
    }
 
    @Override
    public void run() {
        while (!destroy) {
            System.out.printf("Pool#Min:%d,Active:%d,Max:%d,Current:%d,QueueSize:%d\n",
                    this.min, this.active, this.max, this.size, TASK_QUEUE.size());
 
 
            try {
                Thread.sleep(5_000L);
                //让线程池以最大活跃数运行
                if (TASK_QUEUE.size() > active && size < active) {
                    for (int i = size; i < active; i++) {
                        createWorkTask();
                    }
                    System.out.println("The pool incremented to active.");
 
                    size = active;
                    //让线程池以最大线程数运行
                } else if (TASK_QUEUE.size() > max && size < max) {
                    for (int i = size; i < max; i++) {
                        createWorkTask();
                    }
                    System.out.println("The pool incremented to max.");
                    //让size以最大活跃数运行
                    size = max;
                }
 
 
                /**
                 * 释放线程
                 * 加锁 防止submit的时候 有其他的操作
                 */
                synchronized (TASK_QUEUE) {
                    if (TASK_QUEUE.isEmpty() && size > active) {
                        System.out.println("==========Reduce=========");
                        int releaseSize = size - active;
                        for (Iterator<WorkerTask> it = THREAD_QUEUE.iterator(); it.hasNext(); ) {
                            if (releaseSize <= 0) {
                                break;
                            }
                            WorkerTask task = it.next();
                            task.close();
                            task.interrupt();
                            it.remove();
                            releaseSize--;
                        }
 
                        size = active;
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
 
    private enum TaskState {
        FREE, RUNNING, BLOCKED, DEAD
    }
 
    public interface DiscardPolicy {
 
        void discard() throws DiscardException;
    }
 
    public static class DiscardException extends RuntimeException {
 
        public DiscardException(String message) {
            super(message);
        }
    }
 
    private static class WorkerTask extends Thread {
 
        private volatile TaskState taskState = TaskState.FREE;
 
        public WorkerTask(ThreadGroup group, String name) {
            super(group, name);
        }
 
        public TaskState getTaskState() {
            return this.taskState;
        }
 
        public void run() {
            OUTER:
            while (this.taskState != TaskState.DEAD) {
 
                Runnable runnable;
                synchronized (TASK_QUEUE) {
                    while (TASK_QUEUE.isEmpty()) {
                        try {
                            taskState = TaskState.BLOCKED;
                            TASK_QUEUE.wait();
                        } catch (InterruptedException e) {
                            System.out.println("Closed.");
                            break OUTER;
                        }
                    }
 
                    runnable = TASK_QUEUE.removeFirst();
                }
 
                if (runnable != null) {
                    taskState = TaskState.RUNNING;
                    runnable.run();
                    taskState = TaskState.FREE;
                }
            }
        }
 
        public void close() {
            this.taskState = TaskState.DEAD;
        }
    }
 
    public static void main(String[] args) throws InterruptedException {
        SimpleThreadPool threadPool = new SimpleThreadPool();
        for (int i = 0; i < 40; i++) {
 
            int finalI = i;
            threadPool.submit(() -> {
                System.out.println("The runnable " + finalI + " be serviced by " + Thread.currentThread() + " start.");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("The runnable " + finalI + " be serviced by " + Thread.currentThread() + " finished.");
            });
 
            //System.out.println("======================");
 
        }
//        Thread.sleep(4000);
//        threadPool.shutdown();
//        threadPool.submit(() -> System.out.println("============="));
    }
}
?
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
Pool#Min:4,Active:8,Max:12,Current:4,QueueSize:0
The runnable 2 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable 3 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable 1 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable 0 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable 3 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable 4 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable 1 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable 5 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable 2 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable 6 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable 0 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable 7 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable 8 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] start.
The runnable 9 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] start.
The pool incremented to active.
The runnable 10 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] start.
Pool#Min:4,Active:8,Max:12,Current:8,QueueSize:29
The runnable 11 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] start.
The runnable 4 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable 12 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable 7 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable 13 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable 5 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable 6 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable 14 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable 15 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable 9 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] finished.
The runnable 16 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] start.
The runnable 8 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] finished.
The runnable 10 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] finished.
The runnable 18 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] start.
The runnable 17 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] start.
The runnable 11 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] finished.
The runnable 19 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] start.
The runnable 14 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable 20 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable 15 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable 13 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable 22 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable 12 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable 21 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable 23 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable 24 be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] start.
The runnable 25 be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] start.
The runnable 26 be serviced by Thread[SIMPLE_THREAD_POOL-10,5,Pool_Group] start.
The pool incremented to max.
Pool#Min:4,Active:8,Max:12,Current:12,QueueSize:13
The runnable 27 be serviced by Thread[SIMPLE_THREAD_POOL-11,5,Pool_Group] start.
The runnable 18 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] finished.
The runnable 19 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] finished.
The runnable 29 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] start.
The runnable 16 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] finished.
The runnable 30 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] start.
The runnable 17 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] finished.
The runnable 31 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] start.
The runnable 28 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] start.
The runnable 20 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable 32 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable 23 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable 33 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable 21 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable 22 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable 34 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable 35 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable 24 be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] finished.
The runnable 25 be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] finished.
The runnable 36 be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] start.
The runnable 26 be serviced by Thread[SIMPLE_THREAD_POOL-10,5,Pool_Group] finished.
The runnable 37 be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] start.
The runnable 38 be serviced by Thread[SIMPLE_THREAD_POOL-10,5,Pool_Group] start.
The runnable 27 be serviced by Thread[SIMPLE_THREAD_POOL-11,5,Pool_Group] finished.
The runnable 39 be serviced by Thread[SIMPLE_THREAD_POOL-11,5,Pool_Group] start.
The runnable 29 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] finished.
The runnable 31 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] finished.
The runnable 30 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] finished.
The runnable 28 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] finished.
==========Reduce=========
The runnable 35 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable 32 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable 33 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable 34 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
Closed.
Pool#Min:4,Active:8,Max:12,Current:8,QueueSize:0
Closed.
Closed.
Closed.
The runnable 38 be serviced by Thread[SIMPLE_THREAD_POOL-10,5,Pool_Group] finished.
The runnable 36 be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] finished.
The runnable 37 be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] finished.
The runnable 39 be serviced by Thread[SIMPLE_THREAD_POOL-11,5,Pool_Group] finished.
Pool#Min:4,Active:8,Max:12,Current:8,QueueSize:0
Pool#Min:4,Active:8,Max:12,Current:8,QueueSize:0
Pool#Min:4,Active:8,Max:12,Current:8,QueueSize:0

到此这篇关于Java多线程 自定义线程池详情的文章就介绍到这了,更多相关Java多线程 自定义线程池内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://juejin.cn/post/7021394644621590535

延伸 · 阅读

精彩推荐