Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我可以使用线程的名称作为自定义中断标志吗?_Java_Multithreading_Thread Safety - Fatal编程技术网

Java 我可以使用线程的名称作为自定义中断标志吗?

Java 我可以使用线程的名称作为自定义中断标志吗?,java,multithreading,thread-safety,Java,Multithreading,Thread Safety,我有一个有两个线程的程序,t1是超时线程,t2是工作线程,如下所示: public class Test{ static Thread t1,t2; public static void main(String[] args){ t1=new Thread(new Runnable(){ @Override public void run(){ try{

我有一个有两个线程的程序,t1是超时线程,t2是工作线程,如下所示:

public class Test{
    static Thread t1,t2;
    public static void main(String[] args){
        t1=new Thread(new Runnable(){
            @Override
            public void run(){
                try{
                    Thread.sleep(1000);
                }catch(Exception e){
                }
                if(!Thread.currentThread().isInterrupted()){
                    t2.interrupt();
                    System.out.println("request timeout");
                }
            }
        });
        t1.start();

        t2=new Thread(new Runnable(){
            @Override
            public void run(){
                try{
                    Thread.sleep(3000);
                }catch(Exception e){
                }
                if(!Thread.currentThread().isInterrupted()){
                    t1.interrupt();
                    System.out.println("request succeed");
                }
            }
        });
        t2.start();
    }
}
public class Test{
    static Thread t1,t2;
    public static void main(String[] args){
        t1=new Thread(new Runnable(){
            @Override
            public void run(){
                try{
                    Thread.sleep(1000);
                }catch(Exception e){
                }
                if(!Thread.currentThread().getName().equals("")){
                    t2.setName("");
                    t2.interrupt();
                    System.out.println("request timeout");
                }
            }
        },"t1");
        t1.start();

        t2=new Thread(new Runnable(){
            @Override
            public void run(){
                try{
                    Thread.sleep(3000);
                }catch(Exception e){
                }
                if(!Thread.currentThread().getName().equals("")){
                    t1.setName("");
                    t1.interrupt();
                    System.out.println("request succeed");
                }
            }
        },"t2");
        t2.start();
    }
}
我知道它不起作用,因为Thread.currentThread().isInterrupted()始终为false,并且输出为

request timeout
request succeed
我也想要

request timeout

只是。因此,我需要另一个标志来指示线程是否仍在工作。但我不想扩展Thread类来添加自定义布尔属性,我发现Thread有一个name属性,我可以使用name属性作为中断标志,如下所示:

public class Test{
    static Thread t1,t2;
    public static void main(String[] args){
        t1=new Thread(new Runnable(){
            @Override
            public void run(){
                try{
                    Thread.sleep(1000);
                }catch(Exception e){
                }
                if(!Thread.currentThread().isInterrupted()){
                    t2.interrupt();
                    System.out.println("request timeout");
                }
            }
        });
        t1.start();

        t2=new Thread(new Runnable(){
            @Override
            public void run(){
                try{
                    Thread.sleep(3000);
                }catch(Exception e){
                }
                if(!Thread.currentThread().isInterrupted()){
                    t1.interrupt();
                    System.out.println("request succeed");
                }
            }
        });
        t2.start();
    }
}
public class Test{
    static Thread t1,t2;
    public static void main(String[] args){
        t1=new Thread(new Runnable(){
            @Override
            public void run(){
                try{
                    Thread.sleep(1000);
                }catch(Exception e){
                }
                if(!Thread.currentThread().getName().equals("")){
                    t2.setName("");
                    t2.interrupt();
                    System.out.println("request timeout");
                }
            }
        },"t1");
        t1.start();

        t2=new Thread(new Runnable(){
            @Override
            public void run(){
                try{
                    Thread.sleep(3000);
                }catch(Exception e){
                }
                if(!Thread.currentThread().getName().equals("")){
                    t1.setName("");
                    t1.interrupt();
                    System.out.println("request succeed");
                }
            }
        },"t2");
        t2.start();
    }
}
上面有什么问题吗?例如,中是否存在任何并发问题

t2.setName("");
t2.interrupt();

根据
Thread.interrupt()
的JAVA API文档,如果您对处于睡眠模式的线程调用此方法,中断状态将被清除,您将获得InterruptedException。因此,在您的情况下,当您执行t2.interrupt()时,线程2将从睡眠模式中断,一个InterruptedException将由sleep方法抛出(您可以捕获它并处理它)。由于在睡眠线程上调用
interrupt()
时中断状态被清除,因此
thread.isInterrupted()
将始终返回false

因此,为了得到您想要的输出,我的建议是在两个线程中都对InterruptException进行异常处理,并相应地处理打印的消息