厌恶线程类工作正常.我能理解它的过程.然后我改变了
mc.srart()进入mc.run()但没有任何改变,也没有任何错误.
有人可以向我解释一下吗?我们总是可以使用run()而不是start()吗?
public class Main {
public static void main(String[] args) {
Myclass mc = new Myclass();
mc.start();
}
}
class Myclass extends Thread {
public void run() {
for (int i = 0; i < 10; i++) {
System.out.print(i + "--");
}
}
}
Run Code Online (Sandbox Code Playgroud) 再会!我有调度程序,我需要检查它的工作时间。我怎样才能做到这一点?当调度程序工作超过 5 分钟时,我需要从 someFunction() 返回错误
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
public int someFunction() {
ScheduledFuture<Integer> future =
scheduler.schedule(new ScheduledPrinter(), 10, TimeUnit.SECONDS);
if (scheduler.isShutdown()) {
return future.get();
}
}
private class ScheduledPrinter implements Callable<Integer> {
public Integer call() throws Exception {
// do something
if (!serverResponse.getErrorData().equals("QueueIsNotReady")) {
scheduler.shutdown();
return getResponse();
}
return null;
}
}
Run Code Online (Sandbox Code Playgroud) 主线程/调用线程等待其他线程完成任务的最佳方式是什么?我想出了两个选择;可能还有更多。
选项 1 - Thread.sleep()
new Thread(() -> System.out.println("Executed task 1")).start();
new Thread(() -> System.out.println("Executed task 2")).start();
Thread.sleep(1000);
Run Code Online (Sandbox Code Playgroud)
选项 2 - 闩锁
CountDownLatch latch = new CountDownLatch(2);
new Thread(() -> {
System.out.println("Executed task 1");
latch.countDown();
}).start();
new Thread(() -> {
System.out.println("Executed task 2");
latch.countDown();
}).start();
latch.await(1, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
选项 3 - CompletableFuture
CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> System.out.println("Executed task 1"));
CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> System.out.println("Executed task 2"));
CompletableFuture.allOf(future1, future2).join();
Run Code Online (Sandbox Code Playgroud)
选项 4 - ??
当线程被终止 ( Thread.State.TERMINATED) 时,它仍然没有被中断。为什么?
我找到了这个,我找到了这个,但都没有回答我的问题。我已经在 OpenJDK 11 和 Oracle JDK 16 上尝试过这个 - 没有区别,结果相同。
我已经使用 Java 10 多年了,多线程任务对我来说一直很清楚;然而,意外地,我遇到了一些奇怪的事情(对我来说),我很想知道。
我明白了,那private volatile boolean interrupted;是刚刚下课的标志(场)Thread,这我能在结合使用interrupt()时,以代码的一些逻辑了的情况下isInterrupted()获得true。
但是,为什么Thread.State.TERMINATEDstate 不也创建相同的线程interrupted?
public class MainClass {
public static void main(String[] args) throws Exception {
Thread alfa = new Thread(() -> {
for (int i = 0; i < 3; i++) {
System.out.println(i + ". " + Thread.currentThread().getName() + …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用流程构建器在我的 Java 应用程序中执行可视化基本脚本代码。由于用户提供的脚本可能无法及时完成执行,我想提供一种方法来限制此执行时间。在下面的代码中,你可以看到我的逻辑,但它并没有真正做它应该做的。我怎样才能使这个等待工作以限制执行时间?
private void run(String scriptFilePath) throws ScriptPluginException {
BufferedReader input = null;
BufferedReader error = null;
try {
ProcessBuilder p = new ProcessBuilder("cscript.exe", "//U", "\"" + scriptFilePath + "\"");
String path = "";
if (scriptFilePath.indexOf("/") != -1) {
path = scriptFilePath.substring(0, scriptFilePath.lastIndexOf("/"));
}
path += "/" + "tempvbsoutput.txt";
p.redirectOutput(new File(path));
Process pp = p.start();
try {
pp.waitFor(executionTimeout, TimeUnit.MINUTES);
} catch (InterruptedException e) {
SystemLog.writeError(jobId, ScriptConsts.COMPONENT_ID, "VBScriptExecutor", "run", 80401104,
"VB Script executes fail.");
}
if (!pp.isAlive()) {
pp.getOutputStream().close();
} …Run Code Online (Sandbox Code Playgroud) 我开始研究,找出为什么单词"join"用于该Thread.join()方法.实际上,它等待线程结束调用它并暂停主线程等待它,因此没有任何东西可以加入.所以,它的名字应该是这样的:
经过太多研究后我发现了一个简单的句子:
Thread.join();name来自调用线程的概念,等待指定的线程加入它.
我完全无法将上述句子记在心里,也无法理解使用join该方法的背景语境.在这种情况下,"加入"一词代表什么?什么都没有加入其他东西; 相反,它只是暂停调用线程等待线程后跟的join().那么,任何人都可以告诉使用这个join词的方法吗?