English 中文(简体)
java 6 read
原标题:java 6 Thread interrupt

我拿着这部法典:

public class ThreadInteraction {
       public static void main(String[] argv) {
        System.out.println("Create and start a thread t1, then going to sleep...");
        Thread1 t1 = new Thread1();
        t1.start();


        try{
            Thread.sleep(1000);
        }
        catch(InterruptedException e) {
            System.out.println(e.toString());
        }

            //let s interrupt t1
            System.out.println("----------- Interrupt t1");
        t1.interrupt();

        for(int i =0; i<100; i++)
            System.out.println("Main is back");
     }
}

class Thread1 extends Thread {
      public void run() {
         for(int i =0; i<10000; i++)
              System.out.println(i + "thread1");
      }
 }

似乎如<代码>t1.interrupt(>)所示,t t 工作与我的产出一样,所有10 000 t1 印有。 我做了一些错误?

最佳回答

<代码>Thread.interrupt()_ actual don t end any. 这种方法只是为了确定 interrupted的中断状态,但必须加以检查。 这是你如何组织法典,使其发挥作用:

public void run() {
    for (int i = 0; i < 10000; i++) {
        if (interrupted()) break;
        System.out.println(i + "thread1");
    }
}

Thread.interrupted(>>> here clears the打断状态,而它是大韩民国,因为我们直接控制了胎面。 如果你试图在<代码>java.util.con 与此同时发现中断情况。 可控在透水池的透镜之一上运行,因此最好使用<代码>Thread. CurrentThread,isInterrupted();,因为你不知道th的中断政策。

问题回答

Thread.interrupt, 只有当目标在非常具体的国家:

首先,使用这种透镜的检查方法,可能会造成一种安全感被抛弃。

如果这一胎面被阻断,以援引目标类别或加入(或)的(长期)方法、加入(长期)、加入(长期、公开)、睡觉或睡觉(长期、暗中)、这一类方法、中断状态将予以清除,并将受到干扰。

如果在一条可中断的通道上,I/O号行动中堵塞了这一天线,该频道将关闭,将确定星座的中断状态,并接收封闭的ByInterruptException。

如果这一read线在选中被阻断,那么read线的断层状态就会被确定,它将立即从甄选行动中恢复,可能具有非零价值,就像采用挑选器的警示方法一样。

If none of the previous conditions hold then this thread s interrupt status will be set.

如果你希望尽早退出,你就必须在座标上核对<条码>。

for(int i =0; i<10000 && !isInterrupted(); i++)




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签