java线程的中断学习

来源:互联网 发布:php const 编辑:程序博客网 时间:2024/05/18 15:52
<span style="font-size:18px;">package thread.java.test;/** * 在这里练习的是线程的中断 * Thread.interrupt()来设置中断状态是true,当一个线程运行时,另一个线程可以调用另一个 * 线程的interrupt()方法来中断他 * Thread.isInterrupt()来获取线程的中断状态 * Thread.interrupted()这是一个静态的方法,用来获取中断状态,并清除中断状态, * 其获取的是清除之前的值,连续调用两次 ,第二次一定会返回false * @author hello *@version jdk1.8.45 *@since 2015-07-25 * */public class SleepInterrupt implements Runnable {public void run(){try {System.out.println("在run()方法中--这个线程休眠10秒");Thread.sleep(10000);System.out.println("在run()方法中--继续运行");} catch (InterruptedException e) {e.printStackTrace();}System.out.println("在run()方法中--休眠之后继续完成");System.out.println("在run()方法中--正常退出");}       public static void main(String[] args) {SleepInterrupt si=new SleepInterrupt();Thread t=new Thread(si);t.start();try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("在main()方法中--中断其他线程");t.interrupt();System.out.println("在main()中--退出");}}</span>

0 0
原创粉丝点击