在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;
}
}