gpt4 book ai didi

java - 为什么我不能杀死线程?

转载 作者:行者123 更新时间:2023-12-01 16:59:55 24 4
gpt4 key购买 nike

亲爱的大家,我有这种类型的代码:

public class Testimplements Runnable  {

public static void main(String[] args) {

InTheLoop l= new InTheLoop();
Thread th = new Thread(l);
th.start();
th.interrupt();
}

@Override
public void run() {

int count = 0;
for (Integer i = 0; i <=10000000000000000000; i++) {
}
}
}

我知道有办法杀死线程。例如:

 // Example 1
if (Thread.interrupted()){
return;
}


// Example 2
if(flag){ // volatile
return;
}

但是我不能在没有 if 语句的情况下杀死线程吗?

最佳答案

如果确实需要,您可以使用 stop() 方法,但请注意,它本质上是不安全的并且已被弃用。请参阅http://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html了解详情。

关于java - 为什么我不能杀死线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28473771/

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