乐趣区

关于线程:线程多线程和线程池看完这些你就能全部搞懂了

摘要: 一文带你搞懂线程、多线程和线程池。

本文分享自华为云社区《一文搞懂线程与线程池》,原文作者:冰 河。

一. 线程

在操作系统中,线程是比过程更小的可能独立运行的根本单位。同时,它也是 CPU 调度的根本单位。线程自身基本上不领有系统资源,只是领有一些在运行时须要用到的系统资源,例如程序计数器,寄存器和栈等。一个过程中的所有线程能够共享过程中的所有资源。

二. 多线程

多线程能够了解为在同一个程序中可能同时运行多个不同的线程来执行不同的工作,这些线程能够同时利用 CPU 的多个外围运行。多线程编程可能最大限度的利用 CPU 的资源。如果某一个线程的解决不须要占用 CPU 资源时(例如 IO 线程),能够使以后线程让出 CPU 资源来让其余线程可能获取到 CPU 资源,进而可能执行其余线程对应的工作,达到最大化利用 CPU 资源的目标。

三. 线程的实现形式

在 Java 中,实现线程的形式大体上分为三种,通过继承 Thread 类、实现 Runnable 接口,实现 Callable 接口。简略的示例代码别离如下所示。

1)继承 Thread 类代码

package io.binghe.concurrent.executor.test;
/**
 * @author binghe
 * @version 1.0.0
 * @description 继承 Thread 实现线程
 */
public class ThreadTest extends Thread {
    @Override
    public void run() {//TODO 在此写在线程中执行的业务逻辑}
}

2)实现 Runnable 接口的代码

package io.binghe.concurrent.executor.test;
/**
 * @author binghe
 * @version 1.0.0
 * @description 实现 Runnable 实现线程
 */
public class RunnableTest implements Runnable {
    @Override
    public void run() {//TODO 在此写在线程中执行的业务逻辑}
}

3)实现 Callable 接口的代码

package io.binghe.concurrent.executor.test;
​
import java.util.concurrent.Callable;
​
/**
 * @author binghe
 * @version 1.0.0
 * @description 实现 Callable 实现线程
 */
public class CallableTest implements Callable<String> {
    @Override
    public String call() throws Exception {
        //TODO 在此写在线程中执行的业务逻辑
        return null;
    }
}

四. 线程的生命周期

1)生命周期

一个线程从创立,到最终的沦亡,须要经验多种不同的状态,而这些不同的线程状态,由始至终也形成了线程生命周期的不同阶段。线程的生命周期能够总结为下图。

其中,几个重要的状态如下所示。

  • NEW:初始状态,线程被构建,然而还没有调用 start() 办法。
  • RUNNABLE:可运行状态,可运行状态能够包含:运行中状态和就绪状态。
  • BLOCKED:阻塞状态,处于这个状态的线程须要期待其余线程开释锁或者期待进入 synchronized。
  • WAITING:示意期待状态,处于该状态的线程须要期待其余线程对其进行告诉或中断等操作,进而进入下一个状态。
  • TIME_WAITING:超时期待状态。能够在肯定的工夫自行返回。
  • TERMINATED:终止状态,以后线程执行结束。

2)代码示例

为了更好的了解线程的生命周期,以及生命周期中的各个状态,接下来应用代码示例来输入线程的每个状态信息。

WaitingTime

创立 WaitingTime 类,在 while(true) 循环中调用 TimeUnit.SECONDS.sleep(long) 办法来验证线程的 TIMED_WARTING 状态,代码如下所示。

package io.binghe.concurrent.executor.state;
import java.util.concurrent.TimeUnit;
​
/**
 * @author binghe
 * @version 1.0.0
 * @description 线程一直休眠
 */
public class WaitingTime implements Runnable{
    @Override
    public void run() {while (true){waitSecond(200);
        }
    }
    // 线程期待多少秒
    public static final void waitSecond(long seconds){
        try {TimeUnit.SECONDS.sleep(seconds);
        } catch (InterruptedException e) {e.printStackTrace();
        }
    }
}

WaitingState

创立 WaitingState 类,此线程会在一个 while(true) 循环中,获取以后类 Class 对象的 synchronized 锁,也就是说,这个类无论创立多少个实例,synchronized 锁都是同一个,并且线程会处于期待状态。接下来,在 synchronized 中应用以后类的 Class 对象的 wait() 办法,来验证线程的 WAITING 状态,代码如下所示。

package io.binghe.concurrent.executor.state;
/**
 * @author binghe
 * @version 1.0.0
 * @description 线程在 Warting 上期待
 */
public class WaitingState implements Runnable {
    @Override
    public void run() {while (true){synchronized (WaitingState.class){
                try {WaitingState.class.wait();
                } catch (InterruptedException e) {e.printStackTrace();
                }
            }
        }
    }
}

BlockedThread

BlockedThread 次要是在 synchronized 代码块中的 while(true) 循环中调用 TimeUnit.SECONDS.sleep(long) 办法来验证线程的 BLOCKED 状态。当启动两个 BlockedThread 线程时,首先启动的线程会处于 TIMED_WAITING 状态,后启动的线程会处于 BLOCKED 状态。代码如下所示。

package io.binghe.concurrent.executor.state;
/**
 * @author binghe
 * @version 1.0.0
 * @description 加锁后不再开释锁
 */
public class BlockedThread implements Runnable {
    @Override
    public void run() {synchronized (BlockedThread.class){while (true){WaitingTime.waitSecond(100);
            }
        }
    }
}

ThreadState

启动各个线程,验证各个线程输入的状态,代码如下所示。

package io.binghe.concurrent.executor.state;
​
/**
 * @author binghe
 * @version 1.0.0
 * @description 线程的各种状态,测试线程的生命周期
 */
public class ThreadState {
​
    public static void main(String[] args){new Thread(new WaitingTime(), "WaitingTimeThread").start();
        new Thread(new WaitingState(), "WaitingStateThread").start();
​
        //BlockedThread-01 线程会抢到锁,BlockedThread-02 线程会阻塞
        new Thread(new BlockedThread(), "BlockedThread-01").start();
        new Thread(new BlockedThread(), "BlockedThread-02").start();}
}

运行 ThreadState 类,如下所示。

能够看到,未输入任何后果信息。能够在命令行输出“jps”命令来查看运行的 Java 过程。

c:\>jps
21584 Jps
17828 KotlinCompileDaemon
12284 Launcher
24572
28492 ThreadState

能够看到 ThreadSate 过程的过程号为 28492,接下来,输出“jstack 28492”来查看 ThreadSate 过程栈的信息,如下所示。

c:\>jstack 28492
2020-02-15 00:27:08
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.202-b08 mixed mode):
​
"DestroyJavaVM" #16 prio=5 os_prio=0 tid=0x000000001ca05000 nid=0x1a4 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
​
"BlockedThread-02" #15 prio=5 os_prio=0 tid=0x000000001ca04800 nid=0x6eb0 waiting for monitor entry [0x000000001da4f000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at io.binghe.concurrent.executor.state.BlockedThread.run(BlockedThread.java:28)
        - waiting to lock <0x0000000780a7e4e8> (a java.lang.Class for io.binghe.concurrent.executor.state.BlockedThread)
        at java.lang.Thread.run(Thread.java:748)
​
"BlockedThread-01" #14 prio=5 os_prio=0 tid=0x000000001ca01800 nid=0x6e28 waiting on condition [0x000000001d94f000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep(Native Method)
        at java.lang.Thread.sleep(Thread.java:340)
        at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
        at io.binghe.concurrent.executor.state.WaitingTime.waitSecond(WaitingTime.java:36)
        at io.binghe.concurrent.executor.state.BlockedThread.run(BlockedThread.java:28)
        - locked <0x0000000780a7e4e8> (a java.lang.Class for io.binghe.concurrent.executor.state.BlockedThread)
        at java.lang.Thread.run(Thread.java:748)
​
"WaitingStateThread" #13 prio=5 os_prio=0 tid=0x000000001ca06000 nid=0x6fe4 in Object.wait() [0x000000001d84f000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x0000000780a7b488> (a java.lang.Class for io.binghe.concurrent.executor.state.WaitingState)
        at java.lang.Object.wait(Object.java:502)
        at io.binghe.concurrent.executor.state.WaitingState.run(WaitingState.java:29)
        - locked <0x0000000780a7b488> (a java.lang.Class for io.binghe.concurrent.executor.state.WaitingState)
        at java.lang.Thread.run(Thread.java:748)
​
"WaitingTimeThread" #12 prio=5 os_prio=0 tid=0x000000001c9f8800 nid=0x3858 waiting on condition [0x000000001d74f000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep(Native Method)
        at java.lang.Thread.sleep(Thread.java:340)
        at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
        at io.binghe.concurrent.executor.state.WaitingTime.waitSecond(WaitingTime.java:36)
        at io.binghe.concurrent.executor.state.WaitingTime.run(WaitingTime.java:29)
        at java.lang.Thread.run(Thread.java:748)
​
"Service Thread" #11 daemon prio=9 os_prio=0 tid=0x000000001c935000 nid=0x6864 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
​
"C1 CompilerThread3" #10 daemon prio=9 os_prio=2 tid=0x000000001c88c800 nid=0x6a28 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
​
"C2 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x000000001c880000 nid=0x6498 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
​
"C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x000000001c87c000 nid=0x693c waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
​
"C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x000000001c87b800 nid=0x5d00 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
​
"Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000000001c862000 nid=0x6034 runnable [0x000000001d04e000]
   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 <0x0000000780b2fd88> (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 <0x0000000780b2fd88> (a java.io.InputStreamReader)
        at java.io.BufferedReader.readLine(BufferedReader.java:389)
        at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:64)
​
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000000001c788800 nid=0x6794 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
​
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001c7e3800 nid=0x3354 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
​
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000000001c771000 nid=0x6968 in Object.wait() [0x000000001cd4f000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x0000000780908ed0> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
        - locked <0x0000000780908ed0> (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=0x000000001c770800 nid=0x6590 in Object.wait() [0x000000001cc4f000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x0000000780906bf8> (a java.lang.ref.Reference$Lock)
        at java.lang.Object.wait(Object.java:502)
        at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
        - locked <0x0000000780906bf8> (a java.lang.ref.Reference$Lock)
        at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
​
"VM Thread" os_prio=2 tid=0x000000001a979800 nid=0x5c2c runnable
​
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00000000033b9000 nid=0x4dc0 runnable
​
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00000000033ba800 nid=0x6690 runnable
​
"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00000000033bc000 nid=0x30b0 runnable
​
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00000000033be800 nid=0x6f68 runnable
​
"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x00000000033c1000 nid=0x6478 runnable
​
"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x00000000033c2000 nid=0x4fe4 runnable
​
"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x00000000033c5000 nid=0x584 runnable
​
"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x00000000033c6800 nid=0x6988 runnable
​
"VM Periodic Task Thread" os_prio=2 tid=0x000000001c959800 nid=0x645c waiting on condition
​
JNI global references: 12

由以上输入的信息能够看出:名称为 WaitingTimeThread 的线程处于 TIMED_WAITING 状态;名称为 WaitingStateThread 的线程处于 WAITING 状态;名称为 BlockedThread-01 的线程处于 TIMED_WAITING 状态;名称为 BlockedThread-02 的线程处于 BLOCKED 状态。

留神:应用 jps 联合 jstack 命令能够分析线上生产环境的 Java 过程的异样信息。

也能够间接点击 IDEA 下图所示的图表间接打印出线程的堆栈信息。

输入的后果信息与应用“jstack 过程号”命令输入的信息基本一致。

点击关注,第一工夫理解华为云陈腐技术~

退出移动版