public class CloseThread {
private static class Worker extends Thread {
private volatile boolean start = true;
@Override
public void run() {
// TODO Auto-generated method stub
while (start) {
}
}
public void shutdown() {
this.start = false;
}
}
public static void main(String[] args) throws InterruptedException {
Worker wk = new Worker();
wk.start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
wk.shutdown();
}
}
public class CloseThread {
private static class Worker extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
break;
}
}
}
}
public static void main(String[] args) throws InterruptedException {
Worker wk = new Worker();
wk.start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
wk.interrupt();
}
}
原文:https://www.cnblogs.com/Stephanie-boke/p/12319848.html