JVM做了它想做的事情,那么如何预测线程执行的顺序呢?
线程化是指同时执行程序过程以提高应用程序性能的实践。虽然直接在业务应用程序中使用线程并不常见,但它们一直在Java框架中使用。
例如,处理大量信息的框架(如
Spring Batch)使用线程来管理数据。同时操作线程或CPU进程可提高性能,从而实现更快,更高效的程序。
获取源代码
找到你的第一个线程:Java的main()方法
学习Thread该类对于理解线程在Java程序中的工作方式非常有帮助。我们可以通过调用currentThread().getName()方法来访问正在执行的线程,如下所示:
public class MainThread {
public static void main(String... mainThread) {
System.out.println(Thread.currentThread().getName());
}
}复制代码
此代码将打印“main”,标识当前正在执行的线程。知道如何识别正在执行的线程是吸收线程概念的第一步。
Java线程生命周期
使用线程时,了解线程状态至关重要。Java线程生命周期包含六种线程状态:
· New:实例化了一个新的Thread()。
· Runnable:Thread的start()方法被调用。
· Running:start()已调用并且线程正在运行。
· Suspended:线程暂时挂起,可以由另一个线程恢复。
· Blocked:线程正在等待机会运行。当一个线程已经调用synchronized()方法,下一个线程必须等到它完成,就会发生这种情况。
· Terminated:线程执行完成。
图1. Java线程生命周期的六种状态
还有更多关于线程状态的探索和理解,但图1中的信息足以让你解决这个Java挑战。
并发处理:扩展Thread类
最简单的是,通过扩展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。当我们使用new inheritingThread()调用方法start()时,将run()执行方法中的逻辑。
我们还在Thread类构造函数中传递第二个线程的名称,因此输出将是:
main is running. inheritingThread is running.
Runnable接口
你可以实现
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());
}
}复制代码
非守护线程vs守护线程
在执行方面,有两种类型的线程:
· 非守护线程执行直到结束。主线程是非守护程序线程的一个很好的例子。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. 在主线程中开始执行。
2. 打印数字从1到100,000。
3. 主线程中的执行结束,很可能在迭代到100,000之前完成。
最终输出将取决于你的JVM实现。
这让我想到了下一点:线程是不可预测的。
线程优先级和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,我们也不能指望首先执行此线程。相反,执行顺序将是随机的。
常数与枚举
这个Thread类是用Java 1.0引入的。那时,优先级是使用常量而不是枚举来设置的。但是,使用常量存在问题:如果我们传递的优先级数不在1到10的范围内,则该setPriority()方法将抛出
IllegalArgumentException。今天,我们可以使用枚举来解决这个问题。使用枚举使得无法传递非法参数,这既简化了代码又使我们能够更好地控制其执行。
Java线程挑战!
你已经了解了一些关于线程的知识,但这对于这篇文章的Java挑战来说已经足够了。
首先,研究以下代码:
public class ThreadChallenge {
private static int wolverineAdrenaline = 10;
public static void main(String... doYourBest) {
new Motorcycle("Harley Davidson").start();
Motorcycle fastBike = new Motorcycle("Dodge Tomahawk");
fastBike.setPriority(Thread.MAX_PRIORITY);
fastBike.setDaemon(false);
fastBike.start();
Motorcycle yamaha = new Motorcycle("Yamaha YZF");
yamaha.setPriority(Thread.MIN_PRIORITY);
yamaha.start();
}
static class Motorcycle extends Thread {
Motorcycle(String bikeName) { super(bikeName); }
@Override public void run() {
wolverineAdrenaline++;
if (wolverineAdrenaline == 13) {
System.out.println(this.getName());
}
}
}
}复制代码
这段代码的输出是什么?分析代码并根据你学到的内容尝试确定自己的答案。
A. Harley Davidson B. Dodge Tomahawk C. Yamaha YZF D. Indeterminate
刚刚发生了什么?了解线程行为
在上面的代码中,我们创建了三个线程。第一个线程是Harley Davidson,我们为此线程分配了默认优先级。Dodge Tomahawk分配了第二个线程MAX_PRIORITY。第三是Yamaha YZF,与MIN_PRIORITY。然后我们启动了线程。
为了确定线程将运行的顺序,你可能首先注意到Motorcycle类扩展了Thread类,并且我们已经在构造函数中传递了线程名称。我们还用条件覆盖了run()方法:if wolverineAdrenaline is equals to 13。
即使它Yamaha YZF是我们执行顺序中的第三个线程,且MIN_PRIORITY不能保证它将在所有JVM实现的最后执行。
你可能还会注意到,在此示例中,我们将Dodge Tomahawk线程设置为daemon。因为它是一个守护程序线程,Dodge Tomahawk可能永远不会完成执行。但是其他两个线程默认是非守护进程,因此Harley Davidson和Yamaha YZF线程肯定会完成它们的执行。
总之,结果将是D:Indeterminate,因为无法保证线程调度程序将遵循我们的执行顺序或线程优先级。
请记住,我们不能依赖程序逻辑(线程或线程优先级的顺序)来预测JVM的执行顺序。
Java线程常见错误
-
· 调用该run()方法以尝试启动新线程。
-
· 试图启动一个线程两次(这将导致一个IllegalThreadStateException)。
-
· 允许多个进程在不应更改状态时更改对象的状态。
-
· 编写依赖于线程优先级的程序逻辑(你无法预测它)。
-
· 依赖于线程执行的顺序 - 即使我们首先启动一个线程,也不能保证它将首先被执行。
关于Java线程要记住什么
-
· 调用start()方法启动Thread。
-
· 可以直接扩展Thread类以使用线程。
-
· 可以在Runnable接口内实现线程动作。
-
· 线程优先级取决于JVM实现。
-
· 线程行为将始终取决于JVM实现。
-
· 如果非守护程序线程首先结束,则守护程序线程将无法完成。
Learn more about Java threads on JavaWorld
-
· Read the Java 101 threads series to learn more about threads and runnables, thread synchronization, thread scheduling with wait/notify, and thread death.
-
· Modern threading: A Java concurrency primer introduces java.util.concurrent and answers common questions for developers new to Java concurrency.
-
· Modern threading for not-quite-beginners offers more advanced tips and best practices for working with java.util.concurrent.
More from Rafael
-
· Get more quick code tips: Read all the posts in the Java Challengers series.
-
· Build your Java skills: Visit the Java Dev Gym for a code workout.
-
· Want to work on stress free projects and write bug-free code? Visit the NoBugsProject for your copy of No Bugs, No Stress - Create a Life-Changing Software Without Destroying Your Life.
更多文章欢迎访问: http://www.apexyun.com
公众号:银河系1号
联系邮箱:[email protected]
(未经同意,请勿转载)