在Java中,线程的终止方法有以下几种:
public class MyThread extends Thread {
private boolean flag = true;
public void run() {
while (flag) {
// 线程执行的逻辑
}
}
public void stopThread() {
flag = false;
}
}
public class MyThread extends Thread {
public void run() {
while (!Thread.interrupted()) {
// 线程执行的逻辑
}
}
}
public class MyThread extends Thread {
public void run() {
// 线程执行的逻辑
}
}
MyThread myThread = new MyThread();
myThread.start();
myThread.stop();
public class MyThread extends Thread {
public void run() {
// 线程执行的逻辑
return;
}
}