开发者

Thread join on itself

开发者 https://www.devze.com 2023-03-05 15:43 出处:网络
I am in doubt, what happens when a thread joins itself. i.e thread calls the join method on its own. I am not getting any error.

I am in doubt, what happens when a thread joins itself. i.e thread calls the join method on its own. I am not getting any error.

Sample :

public class JoinItself extends Thread {

    public void run() {
        System.out.println("Inside the run method ");
        System.out.println(Thread.currentThread().isAlive());
        for(int i=0;i<5;i++) {
            try {
                System.out.println("Joining itself ...");
                Thread.currentThread().join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {

        JoinItself j = new JoinItself();开发者_开发技巧

        System.out.println(j.isAlive());
        j.start();
        System.out.println(j.isAlive());
        System.out.println("Thread started ...");

    }

}

But Why? Should I get any error?


The concept of a thread joining itself does not make sense.

It happens out that the join() method uses the isAlive() method to determine when to return from the join() method. In the current implementation, it also does not check to see if the thread is joining itself.
In other words, the join() method returns when and only when the thread is no longer alive. This will have the effect of waiting forever.


Should I get any error ?

I wouldn't expect an error. The javadocs for Thread.join() do not say that this is an error, and it is just conceivable that some crazy person may use this as another way of doing a sleep, so an undocumented error would be a bad idea.

I guess that Sun didn't think this was a case that was worth giving special attention to.


The join waits for notification from the other thread. In this case same thread is waiting for itself to notify and hence no notification is received. The program will never end.


we can check the join() source code:

public final void join() throws InterruptedException {
    join(0);
}
public final synchronized void join(long millis)
throws InterruptedException {
    long base = System.currentTimeMillis();
    long now = 0;
    if (millis < 0) {
        throw new IllegalArgumentException("timeout value is negative");
    }
    if (millis == 0) {
        while (isAlive()) {
            wait(0);
        }
    } else {
        while (isAlive()) {
            long delay = millis - now;
            if (delay <= 0) {
                break;
            }
            wait(delay);
            now = System.currentTimeMillis() - base;
        }
    }
}
public final synchronized void join(long millis, int nanos)
throws InterruptedException {
    if (millis < 0) {
        throw new IllegalArgumentException("timeout value is negative");
    }
    if (nanos < 0 || nanos > 999999) {
        throw new IllegalArgumentException(
                            "nanosecond timeout value out of range");
    }
    if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
        millis++;
    }
    join(millis);
}

it depends on how your join yourself thread and the alive() method
may this post can make it clear Quick start Java Thread join

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号