- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
概述
我正在学习和使用 java 中的线程。我刚刚开始研究 join()
方法。据我所知,它让当前线程并强制它等待,直到thread.join()
调用结束/死亡/终止。
这是我尝试用来探索该功能的代码:
ThreadJoinMain
package com.threeadjoin.main;
import sun.nio.ch.ThreadPool;
public class ThreadJoinMain {
public static void main(String[] args) {
CustomThreadOne threadOne = new CustomThreadOne();
Thread t1 = new Thread(threadOne);
t1.setName("Thread 1");
t1.setPriority(10);
Thread t2 = new Thread(threadOne);
t2.setName("Thread 2");
/*Thread t3 = new Thread(threadOne);
t3.setName("Thread 3");*/
try{
t1.join();
//t2.join();
}
catch (InterruptedException e){
e.printStackTrace();
}
t1.start();
t2.start();
//t3.start();
}
}
自定义线程
package com.threeadjoin.main;
public class CustomThreadOne implements Runnable{
@Override
public void run() {
for(int i = 0; i < 10; i ++){
System.out.println("Inside thread: " +
Thread.currentThread().getName() + " value: " + i);
}
}
}
大多数时候它都会给出令人满意的结果,如下所示:
Inside thread: Thread 1 value: 0
Inside thread: Thread 1 value: 1
Inside thread: Thread 1 value: 2
Inside thread: Thread 1 value: 3
Inside thread: Thread 1 value: 4
Inside thread: Thread 1 value: 5
Inside thread: Thread 1 value: 6
Inside thread: Thread 1 value: 7
Inside thread: Thread 1 value: 8
Inside thread: Thread 1 value: 9
Inside thread: Thread 2 value: 0
Inside thread: Thread 2 value: 1
Inside thread: Thread 2 value: 2
Inside thread: Thread 2 value: 3
Inside thread: Thread 2 value: 4
Inside thread: Thread 2 value: 5
Inside thread: Thread 2 value: 6
Inside thread: Thread 2 value: 7
Inside thread: Thread 2 value: 8
但是,如果我连续运行此代码而不进行任何更改或强制重新构建它,有时(尽管很少)输出会像这样:
Inside thread: Thread 1 value: 0
Inside thread: Thread 2 value: 0
Inside thread: Thread 1 value: 1
Inside thread: Thread 2 value: 1
Inside thread: Thread 1 value: 2
Inside thread: Thread 2 value: 2
Inside thread: Thread 1 value: 3
Inside thread: Thread 2 value: 3
Inside thread: Thread 1 value: 4
Inside thread: Thread 2 value: 4
Inside thread: Thread 1 value: 5
Inside thread: Thread 2 value: 5
Inside thread: Thread 1 value: 6
Inside thread: Thread 1 value: 7
Inside thread: Thread 1 value: 8
Inside thread: Thread 2 value: 6
Inside thread: Thread 1 value: 9
Inside thread: Thread 2 value: 7
Inside thread: Thread 2 value: 8
Inside thread: Thread 2 value: 9
或者这个:
Inside thread: Thread 2 value: 0
Inside thread: Thread 1 value: 0
Inside thread: Thread 2 value: 1
Inside thread: Thread 1 value: 1
Inside thread: Thread 2 value: 2
Inside thread: Thread 1 value: 2
Inside thread: Thread 2 value: 3
Inside thread: Thread 1 value: 3
Inside thread: Thread 2 value: 4
Inside thread: Thread 1 value: 4
Inside thread: Thread 2 value: 5
Inside thread: Thread 1 value: 5
Inside thread: Thread 2 value: 6
Inside thread: Thread 1 value: 6
Inside thread: Thread 2 value: 7
Inside thread: Thread 1 value: 7
Inside thread: Thread 2 value: 8
Inside thread: Thread 1 value: 8
Inside thread: Thread 2 value: 9
Inside thread: Thread 1 value: 9
我这里有什么遗漏的吗?
最佳答案
概述
问得好! 是的!它保证按预期工作,并且不依赖于 JVM。但是,我在源代码中发现了很多令人困惑的地方,因此我将使用类似的应用程序逐步了解 join()
的语义。让我们研究下面的示例。
示例应用程序
public static void main(String[] args) {
// Create threads t1 -> t3
Thread t1 = new Thread(threadOne);
t1.setName("Thread 1");
Thread t2 = new Thread(threadOne);
t2.setName("Thread 2");
Thread t3 = new Thread(threadOne);
t3.setName("Thread 3");
//////////// Explanation 1 /////////////
t1.start(); // Begin execution of t1
t2.start(); // Begin execution of t2
//////////// Explanation 2 /////////////
try {
t1.join(); // Force main thread to wait for t1
//////////// Explanation 3 /////////////
t2.join(); // Force main thread to wait for t2
//////////// Explanation 4 /////////////
t3.start(); // Begin execution of t3
t3.join(); // Force main thread to wait for t3
//////////// Explanation 5 /////////////
} catch (InterruptedException e) {
e.printStackTrace();
}
}
这里,这段代码中实际上存在 4 个线程:main
、t1
、t2
、t3
。主线程是应用程序创建并用于运行应用程序的启动线程。
说明1
此时,只有 1 个线程正在执行:main
线程。虽然t1
->t3
已创建,但尚未开始执行。
说明2
这里,我们启动了t1
和t2
,这样就有3个执行线程:t1
、t2
和main
。
说明3
在t1.join()
处,main
线程(或调用线程)等待 t1
的执行完成。完成后,main
线程将继续执行。此时,t2
与 main
和 t1
并行执行。
说明4
main
线程再次等待执行完成,但这次是等待 t2
。一旦完成,main
线程就会解除阻塞并继续。
说明5
main
线程已开始执行t3
,并立即等待其完成。
摘要
总体而言,此示例应用程序产生不确定的结果。无法知道 t1
->t3
何时执行。产生不同的结果是正常的,因为线程每次运行可能会获得不同的 CPU 时间,导致它们在逻辑 block 中进展得更远或更远。我们所知道的是,main
线程将确保 t1
和 t2
在启动 t3
之前完成。此外,所有线程 t1
->t3
将在 main
线程完成之前完成执行。
关于java - Thread 中的 join() 方法是否保证完美工作,还是也依赖于各个 JVM?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52291616/
我想知道如果一个人需要并行处理项目 A、B 和 C,其中 A 依赖于 B,B 依赖于 C,那么完美的工作流程是什么。 目前,我将所有内容都放在一个存储库中,这加快了早期开发速度。所以我的工作目录如下所
尝试测试这款外观精美的 gem - http://icelab.com.au/articles/welcome-to-the-omnisocial/ - 这 promise 可以将 Twitter 和
因为每个版本flutter_localizations来自 SDK 取决于 intl 0.17.0而 fstore 依赖于 intl ^0.16.1 , 禁止来自 SDK 的 flutter_loca
类似于Typescript: Type of a property dependent on another property within the same object我想要一种属性依赖的类型。
我需要计算分页的结果数。 演示查询 select A.order_id, IF( E.assign_date IS NOT NULL AND E.assign_dat
我的表中有一个项目列表,其中包含字段 votes_up 和 votes_down 以及其他字段。现在我想根据这两个字段的功能根据受欢迎程度订购商品。问题在于受欢迎程度的算法取决于 votes_up 的
cloud_firestore:^0.14.4firebase_auth: ^0.18.4+1smooth_star_rating: ^1.0.4地理点:^0.7.1geoflutterfire: ^
是否可以定义两个不同的 PatternLayouts 并依赖于它使用第一个或第二个的应用程序 LogLevel? 示例: 如果我使用默认的 ERROR 级别运行我的应用程序,它应该打印如下日志语句:
我有一个函数 template void frobnicate()做的东西。我需要 T 成为少数几个选择类型之一,并且我需要一些关于这些类型的信息。我通过提供特征来做到这一点: template st
我目前正在开发我的一个项目,它是一个类似于 MSPaint 的 WPF 应用程序。但是,我不使用铅笔工具或类似的工具,而是使用对象(矩形、圆形、三角形等)进行绘画。我使用 Prism 和 MVVM 模
我在 ftable 周围写了一个包装器因为我需要计算许多变量的频率和百分比的平面表: mytable <- function(...) { tab <- ftable(...,
如果可以在 Gradle 中使用来自其他项目的任务,我想尝试一下。假设 ProjectB 是一个 src/main/groovy 包含 com.MyTask 的项目,有父 ProjectA 在 Pro
我需要测试一些依赖于当前上下文的静态方法。现在,我当然可以使用 HttpContextWrapper 从我自己的代码中删除这种依赖性。问题在于我在这些方法中使用的第 3 方 API。他们依赖于 Htt
我正在尝试创建一个通用的结构,具有通用实现特征的界限。特征本身是通用的。这是在 Rust 1.49.0 中。 如果我这样做: trait Foo {} struct Baz> { x: F,
如果 Makefile 本身被更改,一个安全的赌注是将所有目标视为过时的。 有没有聪明的方法来添加这种依赖?有没有其他选择? 最佳答案 一个安全的赌注,但一个可怕的想法。示例:您正在使用 automa
我有一种情况,我需要根据远程文件在 make 中执行规则。这是我正在尝试做的一个例子(实际的 Makefile 在这里不相关的方式要复杂得多): URL = http://cdn.sstatic.ne
这是我的第一个 Spring 应用程序,所以请原谅我对此事的无知。 我在 @Autowired 依赖项上遇到 NullPoinerException。 14:08:48,415 SEVERE [com
我在编程方面没有太多经验,所以这是我的问题: 我正在尝试编写一个转换器应用程序。最后,您可以输入一个数字。然后我有两个组件UIPickerView .使用第一个组件,您可以选择输入格式(例如 °Cel
假设我在 Haxe 中有以下类: class Pair { public var first:U = null; public var second:V = null; pub
在很多与向服务器发送请求相关的问题的回答中,我看到提倡使用 Javascript/AJAX。 从某种意义上说,我仍然是一个纯粹主义者,因为我首先尝试开发无需使用 Javascript/AJAX 即可完
我是一名优秀的程序员,十分优秀!