线程池——ThreadPoolExecutor

1.创建线程的方式?

2.为什么使用线程池?有什么优势?

3.线程池的3个常用方式?

 4.线程池的7大参数?

 5.线程池底层工作原理?

6.线程池的拒绝策略?

7.线程池工作原理的代码验证?

public static void main(String[] args) throws Exception {
        // 创建核心线程数2,最大线程数5,阻塞队列size=3的线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 5, 2, TimeUnit.SECONDS, new LinkedBlockingDeque<>(3), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
        for (int i = 1; i <= 9; i++) {
            final int finalI = i;
            try {
                threadPoolExecutor.execute(() -> {
                    System.out.print("【" + finalI + "】");
                    try { Thread.sleep(2000); } catch (Exception e) { e.printStackTrace(); }
                });
            } catch (Exception e) {
                System.out.print("【异常】");
            }
            Thread.sleep(20);
        }
}
线程池原理验证代码

8.线程池配置合理的参数?N代表cpu核心数

CPU密集:大量的运算,IO操作时间短,CPU Loading比较高

IO密集:大部分状况是CPU在等待IO读写操作,CPU Loading较低

 9.死锁编码及定位分析?使用 jstack [pid] 命令

public class Demo {
    public static void main(String[] args) {
        String resourceA = "lockA";
        String resourceB = "lockB";
        new Thread(() -> {
            synchronized (resourceA) {
                System.out.println(Thread.currentThread().getName()+"\t: 拥有资源A,尝试获取资源B");
                try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }
                synchronized (resourceB) {
                }
            }
        }, "线程A").start();
        new Thread(() -> {
            synchronized (resourceB) {
                System.out.println(Thread.currentThread().getName()+"\t: 拥有资源B,尝试获取资源A");
                try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }
                synchronized (resourceA) {
                }
            }
        }, "线程B").start();
    }
}
死锁代码示例

D:\project\idea\demo>jps -l
20656 org.jetbrains.jps.cmdline.Launcher
9204
1356 org.jetbrains.idea.maven.server.RemoteMavenServer36
16364 sun.tools.jps.Jps
5804 锁.手写一个死锁.Demo

D:\project\idea\demo>jstack 5804
2022-02-10 13:48:02
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.311-b11 mixed mode):

"DestroyJavaVM" #22 prio=5 os_prio=0 tid=0x0000020580bb9800 nid=0x51e4 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"线程B" #21 prio=5 os_prio=0 tid=0x000002059ebe1800 nid=0x4240 waiting for monitor entry [0x0000003f12fff000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at 锁.手写一个死锁.Demo.lambda$main$1(Demo.java:20)
        - waiting to lock <0x000000076be246d0> (a java.lang.String)
        - locked <0x000000076be24708> (a java.lang.String)
        at 锁.手写一个死锁.Demo$$Lambda$2/1078694789.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:748)

"线程A" #20 prio=5 os_prio=0 tid=0x000002059ebdc800 nid=0x1d8c waiting for monitor entry [0x0000003f12eff000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at 锁.手写一个死锁.Demo.lambda$main$0(Demo.java:12)
        - waiting to lock <0x000000076be24708> (a java.lang.String)
        - locked <0x000000076be246d0> (a java.lang.String)
        at 锁.手写一个死锁.Demo$$Lambda$1/990368553.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:748)

"Service Thread" #19 daemon prio=9 os_prio=0 tid=0x000002059e80f800 nid=0x9fc runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C1 CompilerThread11" #18 daemon prio=9 os_prio=2 tid=0x000002059e753000 nid=0x53f4 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C1 CompilerThread10" #17 daemon prio=9 os_prio=2 tid=0x000002059e752800 nid=0x16c0 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C1 CompilerThread9" #16 daemon prio=9 os_prio=2 tid=0x000002059e74f800 nid=0x2dac waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C1 CompilerThread8" #15 daemon prio=9 os_prio=2 tid=0x000002059e754000 nid=0x1200 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread7" #14 daemon prio=9 os_prio=2 tid=0x000002059e751800 nid=0x2ff4 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread6" #13 daemon prio=9 os_prio=2 tid=0x000002059e750000 nid=0x1a98 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread5" #12 daemon prio=9 os_prio=2 tid=0x000002059e74e800 nid=0x4cb4 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread4" #11 daemon prio=9 os_prio=2 tid=0x000002059e755800 nid=0x4e80 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread3" #10 daemon prio=9 os_prio=2 tid=0x000002059e751000 nid=0x4a80 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x000002059e74c000 nid=0x1140 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x000002059e749000 nid=0x4eb4 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x000002059e746800 nid=0x106c waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000002059e745000 nid=0x4548 runnable [0x0000003f11ffe000]
   java.lang.Thread.State: RUNNABLE
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
        at java.net.SocketInputStream.read(SocketInputStream.java:171)
        at java.net.SocketInputStream.read(SocketInputStream.java:141)
        at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
        - locked <0x000000076bd0fcd0> (a java.io.InputStreamReader)
        at java.io.InputStreamReader.read(InputStreamReader.java:184)
        at java.io.BufferedReader.fill(BufferedReader.java:161)
        at java.io.BufferedReader.readLine(BufferedReader.java:324)
        - locked <0x000000076bd0fcd0> (a java.io.InputStreamReader)
        at java.io.BufferedReader.readLine(BufferedReader.java:389)
        at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:61)

"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000002059e6ea000 nid=0x1b50 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000002059e6d6000 nid=0x5178 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000002059baab800 nid=0x3a4 in Object.wait() [0x0000003f11cfe000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x000000076bb88ee0> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
        - locked <0x000000076bb88ee0> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
        at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)

"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x000002059e6b2800 nid=0x1640 in Object.wait() [0x0000003f11bfe000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x000000076bb86c00> (a java.lang.ref.Reference$Lock)
        at java.lang.Object.wait(Object.java:502)
        at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
        - locked <0x000000076bb86c00> (a java.lang.ref.Reference$Lock)
        at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)

"VM Thread" os_prio=2 tid=0x000002059baa5800 nid=0x2cdc runnable

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000020580bd2800 nid=0x14f8 runnable

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x0000020580bd3800 nid=0x2244 runnable

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x0000020580bd5000 nid=0x3884 runnable

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x0000020580bd6800 nid=0x160c runnable

"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x0000020580bd8800 nid=0x3bfc runnable

"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x0000020580bd9800 nid=0x34f4 runnable

"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x0000020580bdc800 nid=0x5070 runnable

"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x0000020580bdd800 nid=0x424c runnable

"GC task thread#8 (ParallelGC)" os_prio=0 tid=0x0000020580bde800 nid=0x50d4 runnable

"GC task thread#9 (ParallelGC)" os_prio=0 tid=0x0000020580be0000 nid=0x409c runnable

"GC task thread#10 (ParallelGC)" os_prio=0 tid=0x0000020580be1000 nid=0x4610 runnable

"GC task thread#11 (ParallelGC)" os_prio=0 tid=0x0000020580be4000 nid=0x52b8 runnable

"GC task thread#12 (ParallelGC)" os_prio=0 tid=0x0000020580be6000 nid=0x5364 runnable

"VM Periodic Task Thread" os_prio=2 tid=0x000002059e86f800 nid=0x5250 waiting on condition

JNI global references: 316


Found one Java-level deadlock:
=============================
"???程B":
  waiting to lock monitor 0x000002059b6c7598 (object 0x000000076be246d0, a java.lang.String),
  which is held by "线??A"
"线程A":
  waiting to lock monitor 0x000002059b6c4d08 (object 0x000000076be24708, a java.lang.String),
  which is held by "线程B"

Java stack information for the threads listed above:
===================================================
"线程B":
        at 锁.手写一个死锁.Demo.lambda$main$1(Demo.java:20)
        - waiting to lock <0x000000076be246d0> (a java.lang.String)
        - locked <0x000000076be24708> (a java.lang.String)
        at 锁.手写一个死锁.Demo$$Lambda$2/1078694789.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:748)
"线程A":
        at 锁.手写一个死锁.Demo.lambda$main$0(Demo.java:12)
        - waiting to lock <0x000000076be24708> (a java.lang.String)
        - locked <0x000000076be246d0> (a java.lang.String)
        at 锁.手写一个死锁.Demo$$Lambda$1/990368553.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:748)

Found 1 deadlock.


D:\project\idea\demo>clear
'clear' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

D:\project\idea\demo>cls

D:\project\idea\demo>jps -l
20656 org.jetbrains.jps.cmdline.Launcher
9204
20040 sun.tools.jps.Jps
1356 org.jetbrains.idea.maven.server.RemoteMavenServer36
5804 锁.手写一个死锁.Demo

D:\project\idea\demo>jstack 5804
2022-02-10 13:57:23
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.311-b11 mixed mode):

"DestroyJavaVM" #22 prio=5 os_prio=0 tid=0x0000020580bb9800 nid=0x51e4 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"线程B" #21 prio=5 os_prio=0 tid=0x000002059ebe1800 nid=0x4240 waiting for monitor entry [0x0000003f12fff000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at 锁.手写一个死锁.Demo.lambda$main$1(Demo.java:20)
        - waiting to lock <0x000000076be246d0> (a java.lang.String)
        - locked <0x000000076be24708> (a java.lang.String)
        at 锁.手写一个死锁.Demo$$Lambda$2/1078694789.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:748)

"线程A" #20 prio=5 os_prio=0 tid=0x000002059ebdc800 nid=0x1d8c waiting for monitor entry [0x0000003f12eff000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at 锁.手写一个死锁.Demo.lambda$main$0(Demo.java:12)
        - waiting to lock <0x000000076be24708> (a java.lang.String)
        - locked <0x000000076be246d0> (a java.lang.String)
        at 锁.手写一个死锁.Demo$$Lambda$1/990368553.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:748)

"Service Thread" #19 daemon prio=9 os_prio=0 tid=0x000002059e80f800 nid=0x9fc runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C1 CompilerThread11" #18 daemon prio=9 os_prio=2 tid=0x000002059e753000 nid=0x53f4 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C1 CompilerThread10" #17 daemon prio=9 os_prio=2 tid=0x000002059e752800 nid=0x16c0 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C1 CompilerThread9" #16 daemon prio=9 os_prio=2 tid=0x000002059e74f800 nid=0x2dac waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C1 CompilerThread8" #15 daemon prio=9 os_prio=2 tid=0x000002059e754000 nid=0x1200 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread7" #14 daemon prio=9 os_prio=2 tid=0x000002059e751800 nid=0x2ff4 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread6" #13 daemon prio=9 os_prio=2 tid=0x000002059e750000 nid=0x1a98 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread5" #12 daemon prio=9 os_prio=2 tid=0x000002059e74e800 nid=0x4cb4 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread4" #11 daemon prio=9 os_prio=2 tid=0x000002059e755800 nid=0x4e80 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread3" #10 daemon prio=9 os_prio=2 tid=0x000002059e751000 nid=0x4a80 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x000002059e74c000 nid=0x1140 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x000002059e749000 nid=0x4eb4 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x000002059e746800 nid=0x106c waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000002059e745000 nid=0x4548 runnable [0x0000003f11ffe000]
   java.lang.Thread.State: RUNNABLE
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
        at java.net.SocketInputStream.read(SocketInputStream.java:171)
        at java.net.SocketInputStream.read(SocketInputStream.java:141)
        at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
        - locked <0x000000076bd0fcd0> (a java.io.InputStreamReader)
        at java.io.InputStreamReader.read(InputStreamReader.java:184)
        at java.io.BufferedReader.fill(BufferedReader.java:161)
        at java.io.BufferedReader.readLine(BufferedReader.java:324)
        - locked <0x000000076bd0fcd0> (a java.io.InputStreamReader)
        at java.io.BufferedReader.readLine(BufferedReader.java:389)
        at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:61)

"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000002059e6ea000 nid=0x1b50 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000002059e6d6000 nid=0x5178 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000002059baab800 nid=0x3a4 in Object.wait() [0x0000003f11cfe000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x000000076bb88ee0> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
        - locked <0x000000076bb88ee0> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
        at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)

"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x000002059e6b2800 nid=0x1640 in Object.wait() [0x0000003f11bfe000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x000000076bb86c00> (a java.lang.ref.Reference$Lock)
        at java.lang.Object.wait(Object.java:502)
        at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
        - locked <0x000000076bb86c00> (a java.lang.ref.Reference$Lock)
        at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)

"VM Thread" os_prio=2 tid=0x000002059baa5800 nid=0x2cdc runnable

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000020580bd2800 nid=0x14f8 runnable

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x0000020580bd3800 nid=0x2244 runnable

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x0000020580bd5000 nid=0x3884 runnable

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x0000020580bd6800 nid=0x160c runnable

"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x0000020580bd8800 nid=0x3bfc runnable

"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x0000020580bd9800 nid=0x34f4 runnable

"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x0000020580bdc800 nid=0x5070 runnable

"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x0000020580bdd800 nid=0x424c runnable

"GC task thread#8 (ParallelGC)" os_prio=0 tid=0x0000020580bde800 nid=0x50d4 runnable

"GC task thread#9 (ParallelGC)" os_prio=0 tid=0x0000020580be0000 nid=0x409c runnable

"GC task thread#10 (ParallelGC)" os_prio=0 tid=0x0000020580be1000 nid=0x4610 runnable

"GC task thread#11 (ParallelGC)" os_prio=0 tid=0x0000020580be4000 nid=0x52b8 runnable

"GC task thread#12 (ParallelGC)" os_prio=0 tid=0x0000020580be6000 nid=0x5364 runnable

"VM Periodic Task Thread" os_prio=2 tid=0x000002059e86f800 nid=0x5250 waiting on condition

JNI global references: 316


Found one Java-level deadlock:
=============================
"???程B":
  waiting to lock monitor 0x000002059b6c7598 (object 0x000000076be246d0, a java.lang.String),
  which is held by "线??A"
"线程A":
  waiting to lock monitor 0x000002059b6c4d08 (object 0x000000076be24708, a java.lang.String),
  which is held by "线程B"

Java stack information for the threads listed above:
===================================================
"线程B":
        at 锁.手写一个死锁.Demo.lambda$main$1(Demo.java:20)
        - waiting to lock <0x000000076be246d0> (a java.lang.String)
        - locked <0x000000076be24708> (a java.lang.String)
        at 锁.手写一个死锁.Demo$$Lambda$2/1078694789.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:748)
"线程A":
        at 锁.手写一个死锁.Demo.lambda$main$0(Demo.java:12)
        - waiting to lock <0x000000076be24708> (a java.lang.String)
        - locked <0x000000076be246d0> (a java.lang.String)
        at 锁.手写一个死锁.Demo$$Lambda$1/990368553.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:748)

Found 1 deadlock.
jstack结果示例

===END===

posted @ 2021-07-04 18:50  一柒微笑  阅读(74)  评论(0编辑  收藏  举报