gpt4 book ai didi

java - 需要停止或终止线程

转载 作者:行者123 更新时间:2023-11-30 09:10:55 26 4
gpt4 key购买 nike

<分区>

因为我已经编写了一个简单的 Java 程序来调用 Thread 。下面是我的代码

public class ThreadPoolForParallelExec {

public static void main(String args[]) {
ExecutorService service = Executors.newFixedThreadPool(5);
for (int i = 0; i < 5; i++) {
service.submit(new Task(i));
}
service.shutdown();
}
}

final class Task implements Runnable {
private int taskId;

public Task(int id) {
this.taskId = id;
}

@Override
public void run() {
myclient.intializeAndConnectRemoteMachine(taskId);
Thread.currentThread().stop();
Thread.currentThread().isInterrupted();
}
}

但是,我需要终止 Executor 或 Thread。我试过 Thread.currentThread().stop();和Thread.currentThread().stop();两者都没有用 :( 你能建议一下吗。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com