Thread类的方法,我有这段代码中断在Java中
Thread1 t1 = new Thread1();//obviously Thread1 extends Thread
t1.start();
System.out.println(t1.isInterrupted());
结果是“假” ;
在另一方面,如果我删除throw new InterruptedException();
,我尝试从主要方法中断它,它给了后面的结果,我预计:
Thread1 t1 = new Thread1();
t1.start();
t1.interrupt();
System.out.println(t1.isInterrupted());
它会返回“真”。
这只是一个一致性内存问题,或者我在代码中做错了什么? 在此先感谢。
编辑:我编辑的问题删除方法连接()如JB Nizet指出仍然存在的问题仍然存在。
什么也正是线程1的代码?它是你的第一个片段中的代码吗? –
缺少最后的“}”,现在编辑。是的,它只是一个简单的测试,以查看在创建的线程的main()方法中还是从run()方法中调用interrupt()是否是相同的事情。 – Rollerball