The real interrupt interrupted the thread method

2010-09-02  来源:本站原创  分类:Java  人气:107 

First of all, there were two codes:

Code 1:

view plaincopy to clipboardprint?
/ / Thread interrupted operations (1)
class MyThread implements Runnable
(
public void run () (
System.out.println ("1, enter the run method");
try (
Thread.sleep (10000); / / sleep method throws an exception interrupt
) Catch (InterruptedException e) (
System.out.println ("2, the thread was interrupted"); / / When sleep is interrupted, then the implementation of the interrupt handling
)
System.out.println ("3, run method of implementation of the complete");
)
)
public class ThreadInterruptDemo01
(
public static void main (String args []) (
MyThread my = new MyThread ();
Thread t = new Thread (my);
t.start ();
try (
Thread.sleep (2000); / / main thread rest for 2 seconds
)
catch (Exception e) ()
t.interrupt (); / / Interrupt thread
)

)

Code 2:

view plaincopy to clipboardprint?
/ / Thread interrupted operations (2) thread is not interrupted, interrupt can interrupt sleep, wait and other methods
class MyThread implements Runnable
(
public void run () (
System.out.println ("1, enter the run method");
for (int i = 0; i <= 10000; i + +) (
System.out.println ("thread in the implementation of "+"--"+ i);
)
System.out.println ("Thread run properly completed");
)
)
public class ThreadInterruptDemo02
(
public static void main (String args []) (
MyThread my = new MyThread ();
Thread t = new Thread (my);
t.start ();

t.interrupt (); / / Interrupt thread
)
)

Operating results through two pieces of code can know one of the interrupt code, while code 2 interrupt does not realize, interrupt and do not break the thread of execution, but the implementation was interrupted sleep. Why, this is the contents of the Interrupt interrupt. interrupt can interrupt sleep, wait and so on, which can be seen through the query api, sleep and wait there InterruptedException (interrupt exception) need to be addressed. Therefore, it must read: interrupt can interrupt the method will throw interrupted exception, this is the actual role of interrupt.

Another point that needs attention, when sleep is interrupted after method, sleep and after the implementation of the content will still be equivalent to the thread is awakened into the normal execution.

This article comes from CSDN blog, reproduced, please indicate the source: http://blog.csdn.net/xiaoya629/archive/2010/04/21/5508011.aspx

相关文章
  • The real interrupt interrupted the thread method 2010-09-02

    First of all, there were two codes: Code 1: view plaincopy to clipboardprint? / / Thread interrupted operations (1) class MyThread implements Runnable ( public void run () ( System.out.println ("1, enter the run method"); try ( Thread.sleep (100

  • Thread class, interrupt, interrupted, isInterrupted method of understanding 2010-03-27

    Reprinted from: http://www.blogjava.net/fhtdy2004/archive/2009/08/22/292181.html Java version of the forum in the school found that many ask about such problems, several methods such as the difference between what you want to see t.interrupt () metho

  • Java how to interrupt a running thread 2010-06-04

    Background interrupt (Interrupt) a thread means that the task before the thread to stop what it is doing, effectively aborting its current operation. Thread is dead, or wait for new tasks, or continue to the next step depends on the application. Alth

  • [# 0x004B] Java Multithreading: Terminating thread method 2010-10-04

    Although the Thread class provides the stop () and suspend () method, but both methods are too rough, if the thread takes up some resources (such as opening a file, creating a database connection or something), direct stop () or suspend () will cause

  • interrupt.interrupted .isInterrupted 区别 2013-04-07

    1.interrupt interrupt方法用于中断线程.调用该方法的线程的状态为将被置为"中断"状态. 注意:线程中断仅仅是置线程的中断状态位,不会停止线程.需要用户自己去监视线程的状态为并做处理.支持线程中断的方法(也就是线程中断后会抛出interruptedException的方法)就是在监视线程的中断状态,一旦线程的中断状态被置为"中断状态",就会抛出中断异常. 2.interrupted 和 isInterrupted 首先看一下该方法的实现: publ

  • Thread interrupt method 2010-05-08

    The following code fragment: Thread thread = new Thread(){ public void run(){ System.out.println("thread"); } }; thread.start(); thread.interrupt(); System.out.println("end"); Call the interrupt method did not result in disruption of r

  • java__ multithreaded brief summary 2010-08-17

    1. Thread is inside a different program execution path. . . 2. Runnable Interface ---- only one run method, inherited after the run method to achieve ---- Thread Thread class also inherits the Runnable Interface 3. Thread class / Runnable implementat

  • Java Thread of the interrupt method uses Xiangjie 2010-04-22

    Thread.interrupt () properly, causing the program can not exit (turn) Original Source: http://blog.chenlb.com/2009/07/incorrect-use-thread-interrupt-cause-not-exit.html Java Thread.interrupt () are used improperly, leading to multi-threaded program c

  • Stop Java threads, be careful interrupt () method 2009-08-10

    ---- Transfer from http://www.blogjava.net/jinfeng_wang/archive/2008/04/27/196477.html Procedures are very simple. However, programmers face, multi-threaded showing a new set of challenges, if not properly addressed, will lead to unexpected behavior

  • How to interrupt a thread running or blocking 2010-04-24

    Background Interrupt (Interrupt) means a thread before the thread to stop its ongoing task of all, effectively aborting its current operation. The result of interruption of the thread is dead, or wait for new tasks, or continue to the next step depen

  • Java thread interrupted right 2010-09-02

    Do not advocate the stop () method of the infamous stop () method to stop the thread does not promote the use of, and reasons for what is it? When a thread object calls stop () method, the thread object to run the thread will immediately stop and thr

  • java interrupt and thread the end of depth 2010-12-13

    Interrupt: And hard interrupt soft interrupt the points. The hardware interrupt caused mostly but there were several peripheral devices is random, so here we only discuss the soft interrupt java program is controllable. Here I have to answer a few qu

  • How to interrupt the thread Android 2011-05-29

    When you use the Thread or AsyncTask in the background to deal with a number of time-consuming operation, you may want to control these threads, including the interrupt thread. In many cases, when the user starts the program, a thread running in the

  • Thread.interrupt () How to use 2010-12-17

    See a blogger, said today: a good memory, not as bad blog. Haha, still pretty reasonable, practical records and regular review. The persistence to write blog written and often come back. Closer to home, study a bit today Thread.interrupt () method, t

  • [Bugfix] a new understanding of the InterruptedException Thread 2010-04-27

    In an earlier blog, the wrong interpretation of the interrupt exception, was also named novice paste, find time to seriously understand the recent thread about this exception, huh, huh. : Original here. Dw on the part below from an article posted on

  • JAVA face questions FAQ Series (10) - the words of multi-threaded JAVA face questions FAQ Collection Series (10) - saying the thread or multi-threaded multi-threaded, multi-task we deal with a powerful tool. Threads and processes are different 2010-05-27

    JAVA face questions FAQ Series (10) - the words of multi-threaded collection JAVA face questions FAQ Series (10) - saying the thread or multi-threaded multi-threaded, multi-task we deal with a powerful tool. Threads and processes are different, each

  • java interview study notes thread 2010-07-17

    There are two ways to achieve thread: 1. Inheritance java.lang.Thread, and rewrite its run () method, into which the main thread of execution. 2. To achieve java.lang.Runnable interface, to achieve its run () method, and into which the main thread of

  • [Transfer] java's interrupt mechanism 2010-10-15

    When the external threads on a thread called thread.interrupt () method after, java language processing mechanism is as follows: If this thread can be interrupted in the state, (call xx.wait (), or Selector.select (), Thread.sleep () and other specif

  • [ZT] how to stop a thread or task 2010-11-12

    source File http://forward.com.au/javaProgramming/HowToStopAThread.html Abstract How to stop a Thread is a perannual question for Java programmers. Finally with the release of Java V5.0 (or V1.5), which incorporates java.util.concurrent, a definitive

  • Multithreaded Java Beginner Guide (4): the life cycle of thread 2010-11-16

    Illness and death and people have the same thread also started to go through (wait), run, hang and stop four different states. These four states are available through the method of Thread class control. Here are the Thread class and the four state-re