katiex7
katiex7

Reputation: 913

Thread join(milliseconds) not killing thread in Java

I am trying to timeout a running Thread with a Thread.join(millis), but it just hangs and does not timeout. What could be the cause of this? Sample code

public static void main(String[] args) throws InterruptedException {
    final Thread t1 = new Thread(() -> {
        for (int i = 0; i < 100000000000000000L; i++) {
        }
    });
    t1.start();
    t1.join(100);
    System.out.println("t1.join called");
}

Upvotes: 1

Views: 1424

Answers (1)

user207421
user207421

Reputation: 310978

Thread join(milliseconds) not killing thread in Java

It's not meant to kill the thread. It is meant to wait for it to exit up to the timeout value, and then to throw InterruptedException to the current thread if the timeout expired. See the Javadoc.

Upvotes: 1

Related Questions