HJW
HJW

Reputation: 23443

Determine thread status with join and InterruptedException?

Seems to be that this method is takes in an array of threads, then determines if they have completed using InterruptedException, which seems plausible to me.

private static void waitUntilAllThreadsFinished(Thread[] threadArr) {
    for(int i=0; i<threadArr.length; i++) {
        try {
            threadArr[i].join();
        } catch (InterruptedException e) { }
        log.debug("thread ["+threadArr[i].getName()+"] have completed");
    }
}

Upvotes: 0

Views: 644

Answers (4)

dragn
dragn

Reputation: 1050

This code does not just determine whether all threads have completed, but waits for all of them to complete. And it's not using InterruptedException to do this. If join() calls on finished (dead) thread, the code just continues on without exception. But it will work, I guess...

Upvotes: 0

ssedano
ssedano

Reputation: 8432

I would not force the throw and catch, since it is not free of cost. The Thread class have methods to access the current state of an instance.

Upvotes: 0

kan
kan

Reputation: 28981

I think getState is more appropriate

Upvotes: 1

Bhaskar
Bhaskar

Reputation: 7523

If you just want to know if the thread has been interrupted , the use public boolean isInterrupted() method on the thread reference. This code is trying to block the current thread on each of the thread's completion, and retrying if it got interrupted in th meanwhile.

Upvotes: 1

Related Questions