一、线程中断
public class InterruptTest { public static void main(String[] args){ MyThread mt = new MyThread("myThread"); mt.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } mt.interrupt(); }}class MyThread extends Thread{ private int i ; public MyThread(String name){ super(name); } public void run(){ while(true){ System.out.println(getName()+"执行了"+(i++)+"次"); } }}
上述代码一直循环执行,这是因为interrupted这是用来判断当前线程是否被中断状态,本身并不中断线程。