在Java中用另一个线程的run方法中断一个线程 [英] interrupt one thread inside another thread's run method in Java

查看:246
本文介绍了在Java中用另一个线程的run方法中断一个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读这篇帖子以及从另一个线程中断一个线程的建议是

I was reading this post and the suggestions given to interrupt one thread from another is

如果正确实施,这里有几种方法应该有效。

" " " Here are a couple of approaches that should work, if implemented correctly.

你可以同时拥有线程定期检查一些常见的标志变量(例如称之为stopNow),并安排两个线程设置它当他们完成时。(标志变量需要是易变的......或者正确同步。)

You could have both threads regularly check some common flag variable (e.g. call it stopNow), and arrange that both threads set it when they finish. (The flag variable needs to be volatile ... or properly synchronized.)

你可以同时拥有线程定期调用Thread.isInterrupted()方法来查看它是否被中断。然后每个线程在完成时需要在另一个上调用Thread.interrupt()。

You could have both threads regularly call the Thread.isInterrupted() method to see if it has been interrupted. Then each thread needs to call Thread.interrupt() on the other one when it finishes." " "

我不明白第二种方法是如何使用 Thread.isInterrupted()
也就是说,如何 Thread-1 Thread.interrupt() > Thread-2 。

I do not understand how the second approach is possible that is using Thread.isInterrupted(). That is, how can Thread-1 call Thread.interrupt() on Thread-2.

考虑这个例子,在main方法中我启动两个线程 t1 t2 。我希望 t1 在达到某个条件后停止 t2 。我怎样才能实现这个目标?

Consider this example, in the main method I start two threads t1 and t2. I want t1 to stop t2 after reaching certain condition. how can I achieve this?

    class Thread1 extends Thread {


        public void run(){
            while (!isDone){
                // do something 
            }
        }        //now interrupt Thread-2
    }

    class Thread2 extends Thread {

        public void run(){
            try {
                     while(!Thread.isInterupted()){
                     //do something;
               }
               catch (InterruptedExecption e){
                    //do something               
                }
       }
}


    public class test {

        public static void main(String[] args){


        try {

            Thread1 t1 = new Thread1();
            Thread2 t2 = new Thread2();
            t1.start();
            t2.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        }


    }


推荐答案

public class test {

private static boolean someCondition = true;


public static void main(String[]args){

    Thread t2 = new Thread(new someOtherClass("Hello World"));
    Thread t1 = new Thread(new someClass(t2));
    t2.start();
    t1.start();
    try {
        t1.join();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

static class someClass implements Runnable{

    Thread stop;
    public someClass(Thread toStop){
        stop = toStop;
    }


    public void run(){
        while(true){
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if(someCondition && !stop.isInterrupted()){

                stop.interrupt();

            } 
        }
    }
}

static class someOtherClass implements Runnable{
    String messageToPrint;

    public someOtherClass(String s){
        messageToPrint = s;
    }

    public void run(){

        while(true){
            try {

                Thread.sleep(500);


            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            System.out.println(messageToPrint);
        }
    }
}

}

这篇关于在Java中用另一个线程的run方法中断一个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆