简体   繁体   中英

what is the point of rejoining the thread to the main-thread after the thread has been interrupted?

I saw the code below from Java tutorial oracle. I was just wondering what is the point of rejoining the thread to the main thread after it has been interrupted?

if(((System.currentTimeMillis() - startTime) > patience) && t.isAlive()){
    threadMessage("tired of waiting");
    t.interrupt();
    t.join();

}

I removed t.join() from the if statement and everything seem to be working exactly the same. So why was the code written the way it did?

public class SimpleThreads {


    static void threadMessage(String message){
        String threadName = Thread.currentThread().getName();
        System.out.format("%s: %s%n", threadName, message);
    }

    private static class MessageLoop implements Runnable {

        @Override 
        public void run (){
            String[] importantInfo = { "dog", "cat", "mice"};

            try {
                for(int i = 0; i < importantInfo.length; i++){
                    Thread.sleep(4000);
                    threadMessage(importantInfo[i]);
                }
            } catch (InterruptedException e){
                threadMessage("i wasn't done!");
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        long patience = 10000;

        threadMessage("starting MessageLoop thread");
        long startTime = System.currentTimeMillis();
        Thread t = new Thread(new MessageLoop());
        t.start();

        threadMessage("waiting for MessageLoop thread to finish");
        while(t.isAlive()){
            threadMessage("still waiting...");
            t.join(1000);

            if(((System.currentTimeMillis() - startTime) > patience) && t.isAlive()){
                threadMessage("tired of waiting");
                t.interrupt();
                t.join();

            }
        }
        threadMessage("finally");
    }


}

When you call

t.join(1000);

Java makes no effort to notify the corresponding thread that you are expecting it to complete within 1000 milliseconds. If that thread is doing blocking IO or is sleeping, like yours is, it will continue doing so.

Here, the tutorial demonstrates a cleaner shutdown pattern. They first interrupt the thread, which will wake up from sleep (if that's what it's executing) and eventually terminate. They then call join() to wait for that termination.

They could have omitted the join but, depending on the type of thread ( daemon? ) and its purpose, that may have put the application in an unpredictable state.


It's important to repeat that interruption is a collaborative effort. If you want clean termination of threads, it has to expose (document well) an appropriate interruption pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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