1.线程创建 start
2.线程终止
public static void main(String[] args) {
Thread thread = new Thread(()->{
while(!Thread.currentThread().isInterrupted()){//默认false
index++;
Thread.interrupted();//复位 默认false;不让线程结束,
}
System.out.println(index);
});
thread.start();
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
thread.interrupt();//终止线程
Thread thread1 = new Thread(()->{
while(!Thread.currentThread().isInterrupted()){//默认false
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {//复位 默认false;不让线程结束,
e.printStackTrace();
//break;
//return;
//Thread.currentThread().interrupt();
}
}
System.out.println(index);
});
thread1.start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
thread1.interrupt();//终止线程
1.线程终止 thread1.interrupt();//终止线程
2线程 内部运行 不一定终止,复位后线程会继续运行
Thread.interrupted();//复位 默认false;不让线程结束,
InterruptedException //复位 默认false;不让线程结束,线程内部继续运行
要想终止 InterruptedException break,return,Thread.currentThread().interrupt();