博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java在sleep时调用interrupt方法
阅读量:4228 次
发布时间:2019-05-26

本文共 2292 字,大约阅读时间需要 7 分钟。

 sleep() & interrupt()
线程A正在使用sleep()暂停着: Thread.sleep(100000);
如果要取消他的等待状态,可以在正在执行的线程里(比如这里是B)调用
    a.interrupt();
令线程A放弃睡眠操作,这里a是线程A对应到的Thread实例

执行interrupt()时,并不需要获取Thread实例的锁定.任何线程在任何时刻,都可以调用其他线程interrupt().当sleep中的线程被调用interrupt()时,就会放弃暂停的状态.并抛出InterruptedException.丢出异常的,是A线程.

情况1:先睡眠后打断,则直接打断睡眠,并且清除停止状态值,使之变成false:

public class MyThread extends Thread{    @Override    public void run() {        super.run();        try{            System.out.println("run begin");            Thread.sleep(200000);            System.out.println("run end");        }catch (InterruptedException e){            System.out.println("在沉睡中被停止!进入catch!"+this.isInterrupted());            e.printStackTrace();        }    }}
public class Run {    public static void main(String[] args){        try{            MyThread thread=new MyThread();            thread.start();            Thread.sleep(200);            thread.interrupt();        }catch (InterruptedException e){            System.out.println("main catch");            e.printStackTrace();        }        System.out.println("end!");    }}
run begin
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.mr.three.MyThread.run(MyThread.java:12)
end!
在沉睡中被停止!进入catch!false

情况2:先打断后睡眠,则直接不睡眠:

public class MyThread extends Thread{    @Override    public void run() {        super.run();        try{            System.out.println("run begin");            for(int i=0;i<500000;i++){                System.out.println(i);            }            Thread.sleep(200000);            System.out.println("run end");        }catch (InterruptedException e){            System.out.println("在沉睡中被停止!进入catch!"+this.isInterrupted());            e.printStackTrace();        }    }}
public class Run {    public static void main(String[] args){        try{            MyThread thread=new MyThread();            thread.start();            Thread.sleep(1);            thread.interrupt();        }catch (InterruptedException e){            System.out.println("main catch");            e.printStackTrace();        }        System.out.println("end!");    }}
499997
499998
499999
在沉睡中被停止!进入catch!false
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.mr.three.MyThread.run(MyThread.java:15)

转载地址:http://tojqi.baihongyu.com/

你可能感兴趣的文章
Professional VSTO 2005 : Visual Studio 2005 Tools for Office
查看>>
Building Flash Web Sites For Dummies
查看>>
Service-Oriented Software System Engineering Challenges and Practices
查看>>
Expert One-on-One: Microsoft Access Application Development
查看>>
Managing the IT Services Process
查看>>
Introduction to Cryptography with Java Applets
查看>>
Advanced Wireless Networks : 4G Technologies
查看>>
Professional Java User Interfaces
查看>>
The Database Hacker's Handbook: Defending Database Servers
查看>>
IT Administrator's Top 10 Introductory Scripts for Windows
查看>>
Algorithms and Data Structures: The Science of Computing
查看>>
ASP.NET 2.0 Cookbook
查看>>
Java I/O
查看>>
Visual C# 2005 Demystified
查看>>
Unlocking Microsoft C# V 2.0 Programming Secrets
查看>>
Programming Excel with VBA and .NET
查看>>
SQL Server 2005 T-SQL Recipes: A Problem-Solution Approach
查看>>
Core Python Programming
查看>>
Creating Database Web Applications with PHP and ASP
查看>>
ASP.NET 2.0 Demystified
查看>>