工作中很少真正用到多线程,毕竟CRUD就完事了,但是作为一名Coder,还是很想弄明白底层的一些东西,之前也看过《Java并发编程实战》,但由于没有总结输出,以至于看了就忘==。前几天在学习JVM的类加载器的时候,发现线程里有一个 getContextClassLoader() 方法,心想着是时候对Thread也来一次全面的认识了(重 新 打 基 础),于是就开始了线程的学习。
Thread类
1、创建线程
在Java中,表示线程的就只有Thread类。
要创建一个线程,主要有两种方式,一种是继承Thread类,重写其run方法。另一种是实现Runnable接口,在通过Thread的构造函数传入,由于创建线程过于基础,这里不做过多记录。
@FunctionalInterface
Runnable是一个函数式接口,打开源代码可以看到类上使用了 @FunctionalInterface 注解,表示这个接口中只有一个抽象方法。
这里简单介绍一下函数式接口。
函数式接口是Java8带来的接口新特性,其特点是要求函数式接口要满足以下特性:
- 接口中有且只有一个 抽象 方法。
- 接口中的静态方法以及 默认方法(被default修饰的方法) 都不是抽象方法
- 接口默认继承的java.lang.Object,如果接口中显式声明覆盖了Object中的方法,该方法也不是抽象方法。
- 如果一个接口符合上述定义,那么无论是否添加 @FunctionalInterface 都没有关系。但加上注解,编译器会对接口进行检查,如果发现接口不符合函数式接口的定义,在编译的时候会报异常。
符合函数式接口的接口,我们可以用lambda表达式来简化对接口的实现。
比如创建线程:
Thread t1 = new Thread(()->{
System.out.println("Hello,Lain!");
},"t1");
t1.start();
看上去是不是有点像通过匿名内部类的方式创建线程?实际上这里调用的是这个构造器:Thread(Runnable,String),而匿名内部类实际上还是相当于继承了Thread,然后重写了其run方法。
创建线程中涉及到的设计模式——策略模式
对于Thread来说,它封装了对线程的抽象,创建线程,启动线程的内部实现是不需要我们去完成的,我们只要把业务代码交给线程就行了。Runnable接口为我们提供了便利。
这里使用到了策略模式,所谓的策略模式,即是定义一系列的算法,把它们一个个封装起来, 并且使它们可相互替换。对于Thread来说,它不管你Runnable里面是什么逻辑,它只管调用里面的run方法,通过这种方式将Thread与业务逻辑松耦合。
Thread的构造器
Thread拥有一系列构造器,以便于在定义Thread的时候,以初始化线程名、线程组(ThreadGroup)、Runnable对象(执行对象),以及线程栈大小(stackSize)。
线程名就不用说了,阅读源码可知,如调用的构造器没有name参数,则会默认的赋予“Thread-” + nextThreadNum()。这个方法是个线程安全的方法,它会统计创建的匿名线程的次数,并以次数作为线程名字的一部分。如果构造器里有name参数,那么这个参数不允许为null,否则会抛出空指针异常。
Runnable对象实现了Runnable接口,封装了需要在线程中处理的业务逻辑。
这里重点提一下线程组(ThreadGroup)以及线程栈大小。
线程组
ThreadGroup是用于管理一组线程的,JVM启动时,会默认创建一个System分组,默认会有五个线程(四个system和一个main线程):
(以下摘录自简书)
Attach Listener:Attach Listener线程是负责接收到外部的命令,而对该命令进行执行的并且吧结果返回给发送者。通常我们会用一些命令去要求jvm给我们一些反 馈信息,如:java -version、jmap、jstack等等。如果该线程在jvm启动的时候没有初始化,那么,则会在用户第一次执行jvm命令时,得到启动。
Signal Dispatcher:前面我们提到第一个Attach Listener线程的职责是接收外部jvm命令,当命令接收成功后,会交给signal dispather线程去进行分发到各个不同的模块处理命令,并且返回处理结果。signal dispather线程也是在第一次接收外部jvm命令时,进行初始化工作。
Finalizer:这个线程也是在main线程之后创建的,其优先级为10,主要用于在垃圾收集前,调用对象的finalize()方法;关于Finalizer线程的几点:
- 只有当开始一轮垃圾收集时,才会开始调用finalize()方法;因此并不是所有对象的finalize()方法都会被执行;
2. 该线程也是daemon线程,因此如果虚拟机中没有其他非daemon线程,不管该线程有没有执行完finalize()方法,JVM也会退出;
3. JVM在垃圾收集时会将失去引用的对象包装成Finalizer对象(Reference的实现),并放入ReferenceQueue,由Finalizer线程来处理;最后将该Finalizer对象的引用置为null,由垃圾收集器来回收;
4. JVM为什么要单独用一个线程来执行finalize()方法呢?如果JVM的垃圾收集线程自己来做,很有可能由于在finalize()方法中误操作导致GC线程停止或不可控,这对GC线程来说是一种灾难;
Reference Handler:VM在创建main线程后就创建线程,其优先级最高,为10,它主要用于处理引用对象本身(软引用、弱引用、虚引用)的垃圾回收问题。
main:程序主线程
我们可以实际验证一下:
package fun.lain.laintest.thread;
import java.util.Arrays;
public class CreateThreadTest {
//https://www.cnblogs.com/noteless/p/10354721.html
public static void main(String[] args) throws InterruptedException {
//输出父线程组中所有线程
CreateThreadTest.printParentGroup("System:");
CreateThreadTest.printGroup("main");
}
public static void printGroup(String threadMark){
Thread[] threads = new Thread[6];
Thread.currentThread().getThreadGroup().enumerate(threads);
System.out.println(threadMark + Arrays.toString(threads));
}
public static void printParentGroup(String threadMark){
Thread[] threads = new Thread[6];
Thread.currentThread().getThreadGroup().getParent().enumerate(threads);
System.out.println(threadMark + Arrays.toString(threads));
}
}
输出结果:
System:[Thread[Reference Handler,10,system], Thread[Finalizer,8,system], Thread[Signal Dispatcher,9,system], Thread[Attach Listener,5,system], Thread[main,5,main], null]
main[Thread[main,5,main], null, null, null, null, null]
可以看到确实是启动了这么些个线程。
如果我们在main方法中新建了线程,且线程没有指定线程组,那么这个线程会被归到main方法所在的线程组中去。
注意: 当程序执行线程的构造器时,就已经为线程对象归好了线程组,并非是在线程启动的时候。也就是说线程默认在哪个线程组,取决于线程对象被定义的线程,而不是线程对象调用start()方法的线程!
我们可以看一下Thread的源代码片段:
public Thread(String name) {
init(null, null, name, 0);
}
//...
private void init(ThreadGroup g, Runnable target, String name,
long stackSize, AccessControlContext acc,
boolean inheritThreadLocals) {
if (name == null) {
throw new NullPointerException("name cannot be null");
}
this.name = name;
Thread parent = currentThread();//native方法,获得当前线程
SecurityManager security = System.getSecurityManager();
if (g == null) {
/* Determine if it's an applet or not */
/* If there is a security manager, ask the security manager
what to do. */
if (security != null) {
g = security.getThreadGroup();
}
/* If the security doesn't have a strong opinion of the matter
use the parent thread group. */
if (g == null) {//当ThreadGroup为空的时候
g = parent.getThreadGroup();//设置为当前线程的线程组
}
//.....
}
可以看到,在构造Thread对象的时候,他所在的线程组就已经设置好了。
然后在启动线程的时候,再将自己加入该线程组。
/**
* Causes this thread to begin execution; the Java Virtual Machine
* calls the <code>run</code> method of this thread.
* <p>
* The result is that two threads are running concurrently: the
* current thread (which returns from the call to the
* <code>start</code> method) and the other thread (which executes its
* <code>run</code> method).
* <p>
* It is never legal to start a thread more than once.
* In particular, a thread may not be restarted once it has completed
* execution.
*
* @exception IllegalThreadStateException if the thread was already
* started.
* @see #run()
* @see #stop()
*/
public synchronized void start() {
/**
* This method is not invoked for the main method thread or "system"
* group threads created/set up by the VM. Any new functionality added
* to this method in the future may have to also be added to the VM.
*
* A zero status value corresponds to state "NEW".
*/
if (threadStatus != 0)
throw new IllegalThreadStateException();
/* Notify the group that this thread is about to be started
* so that it can be added to the group's list of threads
* and the group's unstarted count can be decremented. */
group.add(this);//在这里,将自己添加进了线程组
boolean started = false;
try {
start0();
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
/* do nothing. If start0 threw a Throwable then
it will be passed up the call stack */
}
}
}
最后,当线程结束的时候,线程会从线程组里移除。
下面看一个例子:
package fun.lain.laintest.thread;
import java.util.Arrays;
public class CreateThreadTest {
public static void main(String[] args) throws InterruptedException {
CreateThreadTest.printParentGroup("System:");
CreateThreadTest.printGroup("main");
Thread t1 = new Thread("t1"){
@Override
public void run() {
//再启动一个线程
Thread t2 = new Thread(()->{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
CreateThreadTest.printGroup("T2:");
},"t2");
t2.start();
CreateThreadTest.printGroup("T1:");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t1.start();
//确保线程t1中的t2已经启动
Thread.sleep(500);
CreateThreadTest.printGroup("main pre");
CreateThreadTest.printParentGroup("System pre");
//确保线程t1,t2均启动完成
Thread.sleep(4000);
CreateThreadTest.printGroup("main aft");
CreateThreadTest.printParentGroup("System after");
}
public static void printGroup(String threadMark){
Thread[] threads = new Thread[6];
Thread.currentThread().getThreadGroup().enumerate(threads);
System.out.println(threadMark + Arrays.toString(threads));
}
public static void printParentGroup(String threadMark){
Thread[] threads = new Thread[6];
Thread.currentThread().getThreadGroup().getParent().enumerate(threads);
System.out.println(threadMark + Arrays.toString(threads));
}
}
这里定义了两个静态方法,用来输出当前线程的线程组,以及父线程的线程组内线程的状况。
输出结果如下:
System:[Thread[Reference Handler,10,system], Thread[Finalizer,8,system], Thread[Signal Dispatcher,9,system], Thread[Attach Listener,5,system], Thread[main,5,main], null]
main[Thread[main,5,main], null, null, null, null, null]
T1:[Thread[main,5,main], Thread[t1,5,main], Thread[t2,5,main], null, null, null]
main pre[Thread[main,5,main], Thread[t1,5,main], Thread[t2,5,main], null, null, null]
System pre[Thread[Reference Handler,10,system], Thread[Finalizer,8,system], Thread[Signal Dispatcher,9,system], Thread[Attach Listener,5,system], Thread[main,5,main], Thread[t1,5,main]]
T2:[Thread[main,5,main], Thread[t1,5,main], Thread[t2,5,main], null, null, null]
main aft[Thread[main,5,main], null, null, null, null, null]
System after[Thread[Reference Handler,10,system], Thread[Finalizer,8,system], Thread[Signal Dispatcher,9,system], Thread[Attach Listener,5,system], Thread[main,5,main], null]//线程结束,会从线程组中移除
可以看到,在线程T1中再启动一个线程T2,T2也会被加入main线程组,结合源代码可以知道,这里T2是拿的T1的线程组,而T1又是拿的main线程的,因此只要不指定线程组,无论是在哪里创建多少个线程,他们在线程组里都是平级关系的。
线程组的构造方法
ThreadGroup有两个公开的构造方法:
ThreadGroup(String name);
public ThreadGroup(ThreadGroup parent, String name);
线程组必须要有一个父组,如果我们不指定,那么它会默认使用当前线程的线程组。
/**
* Constructs a new thread group. The parent of this new group is
* the thread group of the currently running thread.
* <p>
* The <code>checkAccess</code> method of the parent thread group is
* called with no arguments; this may result in a security exception.
*
* @param name the name of the new thread group.
* @exception SecurityException if the current thread cannot create a
* thread in the specified thread group.
* @see java.lang.ThreadGroup#checkAccess()
* @since JDK1.0
*/
public ThreadGroup(String name) {
this(Thread.currentThread().getThreadGroup(), name);
}
刚刚的例子,我们可以自己创建一个线程组玩一玩。
public class CreateThreadTest {
public static void main(String[] args) throws InterruptedException {
CreateThreadTest.printParentGroup("System:");
CreateThreadTest.printGroup("main");
//自己创建线程组
ThreadGroup group = new ThreadGroup("Lain");
Thread t1 = new Thread(group,"t1"){
@Override
public void run() {
//再启动一个线程
Thread t2 = new Thread(()->{
//...
输出结果:
System:[Thread[Reference Handler,10,system], Thread[Finalizer,8,system], Thread[Signal Dispatcher,9,system], Thread[Attach Listener,5,system], Thread[main,5,main], null]
main[Thread[main,5,main], null, null, null, null, null]
T1:[Thread[t1,5,Lain], Thread[t2,5,Lain], null, null, null, null]
main pre[Thread[main,5,main], Thread[t1,5,Lain], Thread[t2,5,Lain], null, null, null]
System pre[Thread[Reference Handler,10,system], Thread[Finalizer,8,system], Thread[Signal Dispatcher,9,system], Thread[Attach Listener,5,system], Thread[main,5,main], Thread[t1,5,Lain]]
T2:[Thread[t1,5,Lain], Thread[t2,5,Lain], null, null, null, null]
main aft[Thread[main,5,main], null, null, null, null, null]
System after[Thread[Reference Handler,10,system], Thread[Finalizer,8,system], Thread[Signal Dispatcher,9,system], Thread[Attach Listener,5,system], Thread[main,5,main], null]
可以看到T1,T2都变成我们自定义的组了。
另外我们也可以发现,ThreadGroup的enumerate方法是会遍历整个子树的,会输出组以及子组中的全部线程。
线程组提供了对多个线程的统一管理,我之后会再对这些操作进行详细的探究,今天就到这了。