做者:享学课堂King老师
多线程是指同时执行多个线程以提升应用程序的性能。java
例如,处理大量信息的框架(如Spring批处理)使用线程来管理数据。同时操做线程或CPU进程能够提升性能,从而获得更快、更高效的程序。redis
即便你从未直接使用线程,你也在间接使用它,由于main()方法包含一个主线程。不管什么时候执行该main()方法,你都执行了主线程。tomcat
咱们能够经过调用currentThread().getName()方法来访问正在执行的线程,以下所示:bash
public class MainThread {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName());
}
}
复制代码
此代码将打印“main”,标识当前正在执行的线程。这是学习多线程概念的第一步。多线程
使用线程时,了解线程状态相当重要。Java的线程生命周期包含六种线程状态:架构
New:Thread()已经实例化了一个新的。并发
Runnable接:本Thread的start()方法被调用。负载均衡
Running:start()已调用该方法而且线程正在运行。框架
Suspended:线程暂时挂起,能够由另外一个线程恢复。分布式
Blocked:线程正在等待机会运行。当一个线程已经调用该synchronized()方法而且下一个线程必须等到它完成时,就会发生这种状况。
Terminated:线程的执行完成。
最简单的是,经过扩展Thread类来完成并发处理,以下所示。
public class InheritingThread extends Thread {
InheritingThread(String threadName) {
super(threadName);
}
public static void main(String... inheriting) {
System.out.println(Thread.currentThread().getName() + " is running");
new InheritingThread("inheritingThread").start();
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " is running");
}
}
复制代码
在这里,咱们运行两个线程:MainThread和InheritingThread。当咱们start()使用new 调用方法时inheritingThread(),将run()执行方法中的逻辑。
咱们还在Thread类构造函数中传递第二个线程的名称,所以输出将是:
main is running.inheritingThread is running.
复制代码
你也能够实现Runnable接口,而不是使用继承。Runnable在Thread构造函数内部传递会致使更少的耦合和更大的灵活性。传递以后Runnable,咱们能够start()像上一个示例中那样调用方法:
public class RunnableThread implements Runnable {
public static void main(String... runnableThread) {
System.out.println(Thread.currentThread().getName());
new Thread(new RunnableThread()).start();
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
复制代码
在执行方面,有两种类型的线程:
执行非守护程序线程会一直到结束。主线程自己就是非守护程序线程的一个很好的例子。main()除非System.exit()强制程序完成,不然代码输入将始终执行到最后。
一个守护线程是相反的,是一个不须要一直执行到结束的处理程序。
请记住规则:若是封闭的非守护程序线程在守护程序线程以前结束,则守护程序线程将在结束以前执行。为了更好地理解守护进程和非守护进程线程的关系,请参考如下示例:
import java.util.stream.IntStream;
public class NonDaemonAndDaemonThread {
public static void main(String... nonDaemonAndDaemon) throws InterruptedException {
System.out.println("Starting the execution in the Thread " + Thread.currentThread().getName());
Thread daemonThread = new Thread(() -> IntStream.rangeClosed(1, 100000)
.forEach(System.out::println));
daemonThread.setDaemon(true);
daemonThread.start();
Thread.sleep(10);
System.out.println("End of the execution in the Thread " +
Thread.currentThread().getName());
}
}
复制代码
在这个例子中,我使用了守护程序线程来声明1到100,000的范围,迭代全部这些,而后打印。但请记住,若是非守护进程的主线程首先完成,守护程序线程将没法完成执行。
输出将按以下方式进行:
在主线程中开始执行。
打印数字从1到100,000。
主线程中的执行结束,极可能在迭代到100,000以前完成。
最终输出将取决于你的JVM实现。
事实证实:线程是不可预测的。
可使用该setPriority方法肯定线程执行的优先级,可是如何处理它取决于JVM实现。Linux,MacOS和Windows都有不一样的JVM实现,每一个都将根据本身的默认值处理线程优先级。
可是,你设置的线程优先级确实会影响线程调用的顺序。在Thread类上的三个常数是:
/** * The minimum priority that a thread can have. */
public static final int MIN_PRIORITY = 1;
/** * The default priority that is assigned to a thread. */
public static final int NORM_PRIORITY = 5;
/** * The maximum priority that a thread can have. */
public static final int MAX_PRIORITY = 10;
复制代码
尝试对如下代码运行一些测试,以查看最终的执行优先级
public class ThreadPriority {
public static void main(String... threadPriority) {
Thread moeThread = new Thread(() -> System.out.println("Moe"));
Thread barneyThread = new Thread(() -> System.out.println("Barney"));
Thread homerThread = new Thread(() -> System.out.println("Homer"));
moeThread.setPriority(Thread.MAX_PRIORITY);
barneyThread.setPriority(Thread.NORM_PRIORITY);
homerThread.setPriority(Thread.MIN_PRIORITY);
homerThread.start();
barneyThread.start();
moeThread.start();
}
}
复制代码
即便咱们设置moeThread为MAX_PRIORITY,咱们也不能期望首先执行此线程。相反,执行顺序将是随机的。
可是,使用常量有一个问题:若是传递的优先级数字不在1到10的范围内,setPriority()方法将引起IllegalArgumentException。因此咱们可使用枚举来解决这个问题。使用枚举Enums既简化了代码,又可以更好地控制代码的执行。
咱们已经了解了很多关于线程的知识,若是你想更进一步,研究如下代码:
public class ThreadChallenge {
private static int line = 10;
public static void main(String... doYourBest) {
new tool("Car").start();
tool Bike = new tool("Bike");
Bike.setPriority(Thread.MAX_PRIORITY);
Bike.setDaemon(false);
Bike.start();
tool Train = new tool("Train");
Train.setPriority(Thread.MIN_PRIORITY);
Train.start();
}
static class tool extends Thread {
tool(String Name) { super(Name); }
@Override public void run() {
line++;
if (line == 13) {
System.out.println(this.getName());
}
}
}
}
复制代码
这段代码的输出是什么?根据你上面学的内容分析代码。
A. Car
B. Bike
C. Train
D. 不肯定
在上面的代码中,咱们建立了三个线程。第一个线程是Car,咱们为此线程分配了默认优先级。第二个线程是Bike,分配了MAXPRIORITY优先级。第二个线程是Train,分配了MINPRIORITY优先级。而后咱们开始了多线程。为了肯定线程将运行的顺序,您可能首先注意到tool类扩展了Thread类,而且咱们已经在构造函数中传递了线程名称。咱们还run()中若是line等于13就进行打印。注意!即便Train是咱们执行顺序中的第三个线程,而且MIN_PRIORITY不能保证它将在全部JVM实现的最后执行。您可能还会注意到,在此示例中,咱们将Bike线程设置为守护,因此它是一个守护程序线程,Bike可能永远不会完成执行。可是其余两个线程默认是非守护进程,所以Car和Train线程确定会完成它们的执行。总之,结果将是D:不肯定,由于没法保证线程调度程序将遵循咱们的执行顺序或线程优先级。请记住,若是不借助JUC的工具,咱们不能依赖程序逻辑(线程或线程优先级的顺序)来预测线程的执行顺序。
调用run()方法以尝试启动新线程。
试图启动一个线程两次(这将致使一个IllegalThreadStateException)。
容许多个进程在不该更改时更改对象的状态。
编写依赖于线程优先级的程序逻辑
依赖于线程执行的顺序 - 即便咱们首先启动一个线程,也不能保证它将首先被执行。
调用start()方法启动一个 Thread。
能够Thread直接扩展类以使用线程。
能够在Runnable接口内实现线程动做。
线程优先级取决于JVM实现。
线程行为将始终取决于JVM实现。
若是封闭的非守护程序线程首先结束,则守护程序线程将没法完成。
推荐阅读:
END
欢迎长按下图关注公众号:享学课堂online!
公众号后台回复【java】,获取精选准备的架构学习资料(视频+文档+架构笔记)