带示例的Java线程类void interrupt()方法
线程类voidinterrupt()
软件包java.lang.Thread.interrupt()中提供了此方法。
此方法用于中断线程。
我们将讨论三种情况
如果线程处于睡眠或等待状态,那么在这种情况下,我们可以通过引发异常(InterruptedException)来中断线程执行。
如果线程处于睡眠或等待状态,那么在这种情况下,我们也可以通过调用interrupt()method中断线程以正常执行。
如果线程没有处于睡眠或等待状态,那么在这种情况下,我们还可以通过调用interrupt()method中断线程以正常执行。
此方法不是静态的,因此我们也无法使用类名访问此方法。
此方法的返回类型为void,因此它不返回任何内容。
如果当前线程无法修改该线程,则此方法引发异常。
语法:
void interrupt(){
}参数:
在Thread方法中,我们不传递任何对象作为参数。
返回值:
此方法的返回类型为void,不返回任何内容。
Java程序演示interrupt()方法示例
情况1:如果线程处于睡眠模式,我们正在中断线程以停止执行该线程。
/* We will use Thread class methods so we are importing
the package but it is not mandate because
it is imported by default
*/
import java.lang.Thread;
class InterruptedThread extends Thread
{
//覆盖run() Thread类的方法
public void run()
{
/* The thread named InterruptedThread is in sleep mode
by using sleep(t) so this thread is giving a chance
for execution to another thread named "main"
*/
try{
Thread.sleep(1000);
System.out.println("We are waiting till another thread execution");
}catch(Exception ex){
System.out.println("InterruptedThread is interrupted "+ex.getMessage());
}
}
public static void main(String args[])
{
InterruptedThread it = new InterruptedThread();
/* By using start() method to call the run() method of
Thread class and Thread class start() will call run()
method of InterruptedThread class
*/
it.start();
/* Here thread named InterruptedThread got interrupted by
using the interrupt() method
*/
try
{
it.interrupt();
}catch(Exception ex){
System.out.println("Exception handled "+ex.getMessage());
}
}
}输出结果
E:\Programs>javac InterruptedThread.java
E:\Programs>java InterruptedThread
Exception in thread "Thread-0" java.lang.RuntimeException:
InterruptedThread is Interruptedsleep interrupted
at InterruptedThread.run(InterruptedThread.java:13)情况2:如果线程处于睡眠模式,我们将中断该线程以使其正常执行而不会停止。
/* We will use Thread class methods so we are importing
the package but it is not mandate because
it is imported by default
*/
import java.lang.Thread;
class InterruptedThread extends Thread
{
//覆盖run() Thread类的方法
public void run()
{
/* The thread named InterruptedThread is in sleep mode
by using sleep(t) so this thread is giving a chance
for execution to another thread named "main"
*/
try{
Thread.sleep(1000);
System.out.println("We are waiting till another thread execution");
}catch(Exception ex){
System.out.println("Exception handled : "+ex.getMessage());
}
System.out.println("Interrupted thread is executing normally");
}
public static void main(String args[])
{
InterruptedThread it = new InterruptedThread();
/* By using start() method to call the run() method of
Thread class and Thread class start() will call run()
method of InterruptedThread class
*/
it.start();
/* Here thread named InterruptedThread got interrupted by
using the interrupt() method
*/
it.interrupt();
}
}输出结果
E:\Programs>javac InterruptedThread.java E:\Programs>java InterruptedThread Exception handled : sleep interrupted Interrupted thread is executing normally
情况3:如果线程未处于睡眠模式,则我们将中断该线程的正常执行。
/* We will use Thread class methods so we are importing
the package but it is not mandate because
it is imported by default
*/
import java.lang.Thread;
class InterruptedThread extends Thread
{
//覆盖run() Thread类的方法
public void run()
{
System.out.println("We are executing this thread normally");
}
public static void main(String args[])
{
InterruptedThread it = new InterruptedThread();
/* By using start() method to call the run() method of
Thread class and Thread class start() will call run()
method of InterruptedThread class
*/
it.start();
/* Here thread named InterruptedThread got interrupted by
using the interrupt() method
*/
it.interrupt();
}
}输出结果
E:\Programs>javac InterruptedThread.java E:\Programs>java InterruptedThread We are executing this thread normally