简体   繁体   English

使用另一个线程终止一个线程(循环)

[英]Terminating one thread(looping) using another thread

I am trying to find a way to terminate a thread which is currently looping infinite. 我试图找到一种方法来终止当前循环无限的线程。 With my experience I tried to create a second thread which would interrupt the first one which is looping infinite, but of course due to the infinite loop ... the first thread would never reach the sleep function. 以我的经验,我试图创建一个第二个线程,该线程将中断第一个无限循环的线程,但是当然由于无限循环……第一个线程永远不会到达睡眠功能。 So now I'm back to this 所以现在我回到这个

public class Pulse{

private static int z = 0;

public static void main( String[] args ) throws Exception {
    try {
        final long stop=System.currentTimeMillis()+5L;
            //Creating the 2 threads
        for (int i=0; i<2; i++) {
            final String id=""+i+": ";
            new Thread(new Runnable() {
                public void run() {
                    System.err.println("Started thread "+id);
                    try{
                        while ( System.currentTimeMillis() < stop ) {
                                    //Purposely looping infinite
                            while(true){
                                z++;
                                System.out.println(z);
                            }
                        }
                    } catch (Exception e) {
                        System.err.println(e);
                    }
                }
            }).start();
        }
    } catch (Exception x) {
        x.printStackTrace();
    }
}
}

Have a volatile boolean field, say running . 有一个volatile boolean字段,例如running Make it true . 让它true Where you have while (true) change that to while (running) , and while ( System.currentTimeMillis() < stop ) { to while (running && ( System.currentTimeMillis() < stop) ) { . 您拥有while (true)将其更改为while (running)while ( System.currentTimeMillis() < stop ) {while (running && ( System.currentTimeMillis() < stop) ) { Now, change running to false from some other thread. 现在,改变runningfalse从其他线程。 That should stop the loop fairly nicely. 那应该很好地停止循环。

Can you change 你能改变吗

 while(true){

to

while(!Thread.currentThread().isInterrupted()){ //or Thread.interrupted()

Now when you interrupt the thread it should correctly break out of the infinite loop. 现在,当您中断线程时,它应该正确地摆脱无限循环。

You'll have to do a Thread.interrupted() call inside the loop to check if its been interrupted and handle it appropriately. 您必须在循环内执行Thread.interrupted()调用,以检查其是否被中断并适当地处理它。 Or while(!Thread.interrupted()) instead. 或while(!Thread.interrupted())代替。

You have to do something like this: 您必须执行以下操作:

public class ThreadStopExample {
    public static volatile boolean terminate = false;

    public static void main(String[] args) {
        new Thread(new Runnable() {
            private int i;

            public void run() {
                while (!terminate) {
                    System.out.println(i++);
                }
                System.out.println("terminated");
            }
        }).start();
        // spend some time in another thread
        for (int i = 0; i < 10000; i++) {
            System.out.println("\t" + i);
        }
        // then terminate the thread above
        terminate = true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM