以下是参考<<Java多线程模式>>的

1. sleep() & interrupt()

线程A正在使用sleep()暂停着: Thread.sleep(100000);

如果要取消他的等待状态,可以在正在执行的线程里(比如这里是B)调用

a.interrupt();

令线程A放弃睡眠操作,这里a是线程A对应到的Thread实例

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



2. wait() & interrupt()

线程A调用了wait()进入了等待状态,也可以用interrupt()取消.

不过这时候要小心锁定的问题.线程在进入等待区,会把锁定解除,当对等待中的线程调用interrupt()时(注意是等待的线程调用其自己的interrupt()),会先重新获取锁定,再抛出异常.在获取锁定之前,是无法抛出异常的.



3. join() & interrupt()

当线程以join()等待其他线程结束时,一样可以使用interrupt()取消之.因为调用join()不需要获取锁定,故与sleep()时一样,会马上跳到catch块里. 注意是随调用interrupt()方法,一定是阻塞的线程来调用其自己的interrupt方法.如在线程a中调用来线程t.join().则a会等t执行完后在执行t.join后的代码,当在线程b中调用来a.interrupt()方法,则会抛出InterruptedException



4. interrupt()只是改变中断状态而已

interrupt()不会中断一个正在运行的线程。这一方法实际上完成的是,在线程受到阻塞时抛出一个中断信号,这样线程就得以退出阻塞的状态。更确切的说,如果线程被Object.wait, Thread.join和Thread.sleep三种方法之一阻塞,那么,它将接收到一个中断异常(InterruptedException),从而提早地终结被阻塞状态。

如果线程没有被阻塞,这时调用interrupt()将不起作用;如果被阻塞了,线程就将得到异常(该线程必须事先预备好处理此状况),接着逃离阻塞状态。

线程A在执行sleep,wait,join时,线程B调用A的interrupt方法,的确这一个时候A会有InterruptedException异常抛出来.但这其实是在sleep,wait,join这些方法内部会不断检查中断状态的值,而自己抛出的InterruptedException。

如果线程A正在执行一些指定的操作时如赋值,for,while,if,调用方法等,都不会去检查中断状态,所以线程A不会抛出InterruptedException,而会一直执行着自己的操作.当线程A终于执行到wait(),sleep(),join()时,才马上会抛出InterruptedException.

若没有调用sleep(),wait(),join()这些方法,或是没有在线程里自己检查中断状态自己抛出InterruptedException的话,那InterruptedException是不会被抛出来的.



顺便加个与Thread.sleep()相同效果的代码:

public static void amethod(long x) throws InterruptedExcetion{

if (x != 0) {

Object o = new Object();

synchronized (o) {

o.wait(x);

}

}

}

背景

中断(Interrupt)一个线程意味着在该线程完成任务之前停止其正在进行的一切,有效地中止其当前的操作。线程是死亡、还是等待新的任务或是继续运行至下一步,就取决于这个程序。虽然初次看来它可能显得简单,但是,你必须进行一些预警以实现期望的结果。你最好还是牢记以下的几点告诫。



    首先,忘掉Thread.stop方法。虽然它确实停止了一个正在运行的线程,然而,这种方法是不安全也是不受提倡的,这意味着,在未来的JAVA版本中,它将不复存在。

===================================================================================================

一些轻率的家伙可能被另一种方法Thread.interrupt所迷惑。尽管,其名称似乎在暗示着什么,然而,这种方法并不会中断一个正在运行的线程(待会将进一步说明),正如Listing A中描述的那样。它创建了一个线程,并且试图使用Thread.interrupt方法停止该线程。Thread.sleep()方法的调用,为线程的初始化和中止提供了充裕的时间。线程本身并不参与任何有用的操作。

class Example1 extends Thread {

            boolean stop=false;
public static void main( String args[] ) throws Exception {
Example1 thread = new Example1();
System.out.println( "Starting thread..." );
thread.start();
Thread.sleep( 3000 );
System.out.println( "Interrupting thread..." );
thread.interrupt();
Thread.sleep( 3000 );
System.out.println("Stopping application..." );
//System.exit(0);
}
public void run() {
while(!stop){
System.out.println( "Thread is running..." );
long time = System.currentTimeMillis();
while((System.currentTimeMillis()-time < 1000)) {
}
}
System.out.println("Thread exiting under request..." );
}
}

如果你运行了Listing A中的代码,你将在控制台看到以下输出:



Starting thread...



Thread is running...



Thread is running...



Thread is running...



Interrupting thread...



Thread is running...



Thread is running...



Thread is running...



Stopping application...



Thread is running...



Thread is running...



Thread is running...

............................... 

甚至,在Thread.interrupt()被调用后,线程仍然继续运行。

===================================================================================================



真正地中断一个线程



    中断线程最好的,最受推荐的方式是,使用共享变量(shared variable)发出信号,告诉线程必须停止正在运行的任务。线程必须周期性的核查这一变量(尤其在冗余操作期间),然后有秩序地中止任务。Listing B描述了这一方式。

// run()方法里面写线程要执行的任务,在main里面,有两个线程,一个主线程main,一个自定义线程类thread。

在main启动后,thread.start()语句之后,thread线程启动。

Listing B

class Example2 extends Thread {



  volatile boolean stop = false;



  public static void main( String args[] ) throws Exception {



    Example2 thread = new Example2();



   System.out.println( "Starting thread..." );



   thread.start();



   Thread.sleep( 3000 );  //线程阻塞3000haomiao



   System.out.println( "Asking thread to stop..." );



   thread.stop = true;     //stop是thread线程的成员变量 



   Thread.sleep( 3000 );



   System.out.println( "Stopping application..." );



   //System.exit( 0 );



  }

public void run()

{

while ( !stop ) {



     System.out.println( "Thread is running..." );



      long time = System.currentTimeMillis();



      while ( (System.currentTimeMillis()-time < 1000) && (!stop) ) {



      }



    }



   System.out.println( "Thread exiting under request..." );



  }



}



运行Listing B中的代码将产生如下输出(注意线程是如何有秩序的退出的)



Starting thread...



Thread is running...



Thread is running...



Thread is running...



Asking thread to stop...



Thread exiting under request...



Stopping application...

虽然该方法要求一些编码,但并不难实现。同时,它给予线程机会进行必要的清理工作,这在任何一个多线程应用程序中都是绝对需要的。请确认将共享变量定义成volatile 类型或将对它的一切访问封入同步的块/方法(synchronized blocks/methods)中。



到目前为止一切顺利!但是,当线程等待某些事件发生而被阻塞,又会发生什么?当然,如果线程被阻塞,它便不能核查共享变量,也就不能停止。这在许多情况下会发生,例如调用Object.wait()、ServerSocket.accept()和DatagramSocket.receive()时,这里仅举出一些。



他们都可能永久的阻塞线程。即使发生超时,在超时期满之前持续等待也是不可行和不适当的,所以,要使用某种机制使得线程更早地退出被阻塞的状态。



很不幸运,不存在这样一种机制对所有的情况都适用,但是,根据情况不同却可以使用特定的技术。在下面的环节,我将解答一下最普遍的例子。



使用Thread.interrupt()中断线程



  正如Listing A中所描述的,Thread.interrupt()方法不会中断一个正在运行的线程。这一方法实际上完成的是,在线程受到阻塞时抛出一个中断信号,这样线程就得以退出阻塞的状态。更确切的说,如果线程被Object.wait, Thread.join和Thread.sleep三种方法之一阻塞,那么,它将接收到一个中断异常(InterruptedException),从而提早地终结被阻塞状态。

===================================================================================================



    因此,如果线程被上述几种方法阻塞,正确的停止线程方式是设置共享变量,并调用interrupt()(注意变量应该先设置)。如果线程没有被阻塞,这时调用interrupt()将不起作用;如果线程被阻塞了,线程就将得到异常(该线程必须事先预备好处理此状况),接着结束阻塞状态。在任何一种情况中,最后线程都将检查共享变量然后再停止。Listing C这个示例描述了该技术。



Listing C

class Example3 extends Thread {



  volatile boolean stop = false;



  public static void main( String args[] ) throws Exception {



   Example3 thread = new Example3();



   System.out.println( "Starting thread..." );



   thread.start();



   Thread.sleep( 3000 );  //阻塞进程



   System.out.println( "Asking thread to stop..." );



   thread.stop = true;   //如果线程阻塞,将不会检查此成员变量



   thread.interrupt();     //如果线程被阻塞,调用interrupt()将会收到中断异常



   Thread.sleep( 3000 );  //阻塞进程



   System.out.println( "Stopping application..." );



   //System.exit( 0 );



  }



  public void run() {



    while ( !stop )

{



     System.out.println( "Thread running..." );



      try {



      Thread.sleep( 1000 );



      } catch ( InterruptedException e ) {



      System.out.println( "Thread interrupted..." );



      }



    }



   System.out.println( "Thread exiting under request..." );



  }



}



一旦Listing C中的Thread.interrupt()被调用,线程便收到一个异常,于是逃离了阻塞状态并确定应该停止。运行以上代码将得到下面的输出:



Starting thread...



Thread running...



Thread running...



Thread running...



Asking thread to stop...



Thread interrupted...



Thread exiting under request...



Stopping application...



===================================================================================================

中断I/O操作

    然而,如果线程在I/O操作进行时被阻塞,又会如何?I/O操作可以阻塞线程一段相当长的时间,特别是牵扯到网络应用时。例如,服务器可能需要等待一个请求(request),又或者,一个网络应用程序可能要等待远端主机的响应。



如果你正使用通道(channels)(这是在Java 1.4中引入的新的I/O API),那么被阻塞的线程将收到一个ClosedByInterruptException异常。如果情况是这样,其代码的逻辑和第三个例子中的是一样的,只是异常不同而已。



但是,你可能正使用Java1.0之前就存在的传统的I/O,而且要求更多的工作。既然这样,Thread.interrupt()将不起作用,因为线程将不会退出被阻塞状态。Listing D描述了这一行为。尽管interrupt()被调用,线程也不会退出被阻塞状态



Listing D

import java.io.*;



class Example4 extends Thread {



  public static void main( String args[] ) throws Exception {



    Example4 thread = new Example4();



   System.out.println( "Starting thread..." );



   thread.start();



   Thread.sleep( 3000 );



   System.out.println( "Interrupting thread..." );



   thread.interrupt();



   Thread.sleep( 3000 );



   System.out.println( "Stopping application..." );



   //System.exit( 0 );



  }



  public void run() {



   ServerSocket socket;



    try {



      socket = new ServerSocket(7856);



    } catch ( IOException e ) {



     System.out.println( "Could not create the socket..." );



      return;



    }



    while ( true ) {



     System.out.println( "Waiting for connection..." );



      try {



       Socket sock = socket.accept();



      } catch ( IOException e ) {



      System.out.println( "accept() failed or interrupted..." );



      }



    }



  }



}





   很幸运,Java平台为这种情形提供了一项解决方案,即调用阻塞该线程的套接字的close()方法。在这种情形下,如果线程被I/O操作阻塞,该线程将接收到一个SocketException异常,这与使用interrupt()方法引起一个InterruptedException异常被抛出非常相似。



唯一要说明的是,必须存在socket的引用(reference),只有这样close()方法才能被调用。这意味着socket对象必须被共享。Listing E描述了这一情形。运行逻辑和以前的示例是相同的。



Listing E

import java.net.*;

import java.io.*;

class Example5 extends Thread {

  volatile boolean stop = false;

  volatile ServerSocket socket;

  public static void main( String args[] ) throws Exception {

    Example5 thread = new Example5();

   System.out.println( "Starting thread..." );

   thread.start();

   Thread.sleep( 3000 );

   System.out.println( "Asking thread to stop..." );

   thread.stop = true;

   thread.socket.close();

   Thread.sleep( 3000 );

   System.out.println( "Stopping application..." );

   //System.exit( 0 );

  }

  public void run() {

    try {

      socket = new ServerSocket(7856);

    } catch ( IOException e ) {

     System.out.println( "Could not create the socket..." );

      return;

    }

    while ( !stop ) {

     System.out.println( "Waiting for connection..." );

      try {

       Socket sock = socket.accept();

      } catch ( IOException e ) {

      System.out.println( "accept() failed or interrupted..." );

      }

    }

   System.out.println( "Thread exiting under request..." );

  }

}

以下是运行Listing E中代码后的输出:



Starting thread...



Waiting for connection...



Asking thread to stop...



accept() failed or interrupted...



Thread exiting under request...



Stopping application...



多线程是一个强大的工具,然而它正呈现出一系列难题。其中之一是如何中断一个正在运行的线程。如果恰当地实现,使用上述技术中断线程将比使用Java平台上已经提供的内嵌操作更为简单。

============================================



Writing multithreaded programs in Java, with its built-in support for threads, is fairly straightforward. However, multithreading presents a whole set of new challenges to the programmer
that, if not correctly addressed, can lead to unexpected behavior and subtle, hard-to-find errors. In this article, we address one of those challenges: how to interrupt a running thread.



Background

Interrupting a thread means stopping what it is doing before it has completed its task, effectively aborting its current operation. Whether the thread dies, waits for new tasks, or goes
on to the next step depends on the application.



Although it may seem simple at first, you must take some precautions in order to achieve the desired result. There are some caveats you must be aware of as well.



First of all, forget the Thread.stop method.
Although it indeed stops a running thread, the method is unsafe and was deprecated,
which means it may not be available in future versions of the Java.



Another method that can be confusing for the unadvised is Thread.interrupt.
Despite what its name may imply, the method does not interrupt a running thread (more on this later), as Listing
A
 demonstrates. It creates a thread and tries to stop it using Thread.interrupt.
The calls to Thread.sleep() give plenty of time for the thread initialization and termination.
The thread itself does not do anything useful.



If you run the code in Listing A, you should see something like this on your console:

Starting thread...

Thread is running...

Thread is running...

Thread is running...

Interrupting thread...

Thread is running...

Thread is running...

Thread is running...

Stopping application...



Even after Thread.interrupt() is
called, the thread continues to run for a while.



Really interrupting a thread

The best, recommended way to interrupt a thread is to use a shared variable to signal that it must stop what it is doing. The thread must check the variable periodically, especially
during lengthy operations, and terminate its task in an orderly manner. Listing
B
 demonstrates this technique.



Running the code in Listing B will generate output like this (notice how the thread exits in an orderly fashion):

Starting thread...

Thread is running...

Thread is running...

Thread is running...

Asking thread to stop...

Thread exiting under request...

Stopping application...



Although this method requires some coding, it is not difficult to implement and give the thread the opportunity to do any cleanup needed, which is an absolute requirement for any multithreaded
application. Just be sure to declare the shared variable as volatile or enclose any access to
it into synchronized blocks/methods.



So far, so good! But what happens if the thread is blocked waiting for some event? Of course, if the thread is blocked, it can't check the shared variable and can't stop. There are plenty
of situations when that may occur, such as calling Object.wait(), ServerSocket.accept(),
and DatagramSocket.receive(), to name a few.



They all can block the thread forever. Even if a timeout is employed, it may not be feasible or desirable to wait until the timeout expires, so a mechanism to prematurely exit the blocked
state must be used.



Unfortunately there is no such mechanism that works for all cases, but the particular technique to use depends on each situation. In the following sections, I'll give solutions for the
most common cases.



Interrupting a thread with Thread.interrupt()

As demonstrated in Listing A, the method Thread.interrupt() does
not interrupt a running thread. What the method actually does is to throw an interrupt if the thread is blocked, so that it exits the blocked state. More precisely, if the thread is blocked at one of the methods Object.wait, Thread.join,
or Thread.sleep, it receives anInterruptedException,
thus terminating the blocking method prematurely.



So, if a thread blocks in one of the aforementioned methods, the correct way to stop it is to set the shared variable and then call the interrupt() method
on it (notice that it is important to set the variable first). If the thread is not blocked, calling interrupt() will
not hurt; otherwise, the thread will get an exception (the thread must be prepared to handle this condition) and escape the blocked state. In either case, eventually the thread will test the shared variable and stop. Listing
C
 is a simple example that demonstrates this technique.



As soon as Thread.interrupt() is
called in Listing C, the thread gets an exception so that it escapes the blocked state and determines that it should stop. Running this code produces output like this:

Starting thread...

Thread running...

Thread running...

Thread running...

Asking thread to stop...

Thread interrupted...

Thread exiting under request...

Stopping application...



Interrupting an I/O operation

But what happens if the thread is blocked on an I/O operation? I/O can block a thread for a considerable amount of time, particularly if network communication is involved. For example,
a server may be waiting for a request, or a network application may be waiting for an answer from a remote host.



If you're using channels, available with the new I/O API introduced in Java 1.4, the blocked thread will get a ClosedByInterruptException exception.
If that is the case, the logic is the same as that used in the third example—only the exception is different.



But you might be using the traditional I/O available since Java 1.0, since the new I/O is so recent and requires more work. In this case, Thread.interrupt() doesn't
help, since the thread will not exit the blocked state. Listing
D
 demonstrates that behavior. Although the interrupt() method
is called, the thread does not exit the blocked state.



Fortunately, the Java Platform provides a solution for that case by calling the close() method
of the socket the thread is blocked in. In this case, if the thread is blocked in an I/O operation, the thread will get a SocketException exception,
much like the interrupt() method causes an InterruptedException to
be thrown.



The only caveat is that a reference to the socket must be available so that its close() method
can be called. That means the socket object must also be shared. Listing
E
demonstrates this case. The logic is the same as in the examples presented so far.



And here's the sample output you can expect from running Listing E:

Starting thread...

Waiting for connection...

Asking thread to stop...

accept() failed or interrupted...

Thread exiting under request...

Stopping application...



Multithreading is a powerful tool, but it presents its own set of challenges. One of these is how to interrupt a running thread. If properly implemented, these techniques make interrupting
a thread no more difficult than using the built-in operations already provided by the Java Platform.

中断线程Interrupt()的更多相关文章

  1. “全栈2019”Java多线程第六章:中断线程interrupt()方法详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  2. 并发和多线程(二)--启动和中断线程(Interrupt)的正确姿势

    启动线程: 从一个最基本的面试题开始,启动线程到底是start()还是run()? Runnable runnable = () -> System.out.println(Thread.cur ...

  3. Java并发编程(二)线程任务的中断(interrupt)

    使用interrupt()中断线程 当一个线程运行时,另一个线程可以调用对应的Thread对象的interrupt()方法来中断它,该方法只是在目标线程中设置一个标志,表示它已经被中断,并立即返回. ...

  4. 注意Thread.interrupt()方法的真正作用并不是用来中断线程

      程序是很简易的.然而,在编程人员面前,多线程呈现出了一组新的难题,如果没有被恰当的解决,将导致意外的行为以及细微的.难以发现的错误.      在本篇文章中,我们针对这些难题之一:如何中断一个正在 ...

  5. Interrupt中断线程注意点

    首先我们要明确,线程中断并不会使线程立即退出,而是发送一个通知,告知目标线程你该退出了,但是后面如何处理,则完全有目标线程自行决定. 这就是和stop()不一样的地方,stop执行后线程会立即终止,这 ...

  6. 中断线程详解(Interrupt)

    在上篇文章<多线程的使用——Thread类和Runnable接口>中提到中断线程的问题.在JAVA中,曾经使用stop方法来停止线程,然而,该方法具有固有的不安全性,因而已经被抛弃(Dep ...

  7. 线程中断方法interrupt() 与 cancel()

    (一).关于interrupt()     interrupt()并不直接中断线程,而是设定一个中断标识,然后由程序进行中断检查,确定是否中断.     1. sleep() & interr ...

  8. Interrupt中断线程

    package com.wistron.swpc.ecs.util; public class WrongWayStopThread extends Thread{ public static voi ...

  9. 从头认识java-17.2 线程中断(interrupt)

    这一章节我们来讨论一下线程中断(interrupt). 1.什么是线程中断(interrupt)? 就是在多线程执行的时候,我们给线程贴上一个中断的标记.可是不要求线程终止. 2.样例: 中断的样例: ...

随机推荐

  1. 【JavaScript 2—基础知识点】:数据类型

    导读:我发现不管是哪一门语言,都会先介绍其发展,语法规则,数据类型,流程控制等.那么,这次,就介绍一下JavaScript中的数据类型,有些看着眼熟,有些不熟.熟的也不是之前认识的,不熟的,也不见得就 ...

  2. 【UML】9种图+包图

    导读:在UML的学习中,介绍了9种图,外加一个包图.这9种图和4大关系,可以说是UML的一个核心内容.我根据自己的笔记,以及查阅的一些资料,对这9种图和包图,做一个总结. 一.基本定义 1.1  总体 ...

  3. EasyUI 动态更新列

    function UpdateRow() { var rows = $('#tbpmgridList').datagrid('getChecked'); var productid = ''; for ...

  4. CSS相对布局和绝对布局

    relative 相对布局,正常的,从上到下.绝对布局absolute,就像不占位置,透明了一样,会和别的重合

  5. 常州模拟赛d3t2 灰狼呼唤着同胞

    题目背景 我的母亲柯蒂丽亚,是一个舞者.身披罗纱,一身异国装扮的她,来自灰狼的村子. 曾经在灰狼村子担任女侍的她,被认定在某晚犯下可怕的罪行之后,被赶出了村子. 一切的元凶,都要回到母亲犯下重罪的那一 ...

  6. spring和mybatis整合配置文件

    查看所有springmvc  spring  mybatis配置文件见下链接: https://my.oschina.net/sherwayne/blog/262616 <?xml versio ...

  7. Laravel 5 Form 和 HTML 的使用

    最近在用 laravel 5 做例子,在做到表单的时候,习惯性的使用 Form::open() 结果发现提示错误,没有这个类, 好吧,找了找,发现 在laravel 5 中,把 from 和 html ...

  8. .net面试题汇总一第一篇

    1. 简述 private. protected. public. internal 修饰符的访问权限. private:私有成员,只能在类内部中才可以访问. protected:受保护的,只能在该类 ...

  9. 玩转css样式选择器----当父元素有多个子元素时选中第一个

  10. linux 抓取访问量排行

    需求: 分析图片服务日志,把日志(每个图片访问次数*图片大小的总和)排行,取top10,也就是计算每个url的总访问大小 语句: awk '{a[$1]+=$10;}END{for(i in a){p ...