Java 线程自身连接

Java 线程自身连接,java,multithreading,Java,Multithreading,我很怀疑,当一根线连在一起时会发生什么。i、 e线程自己调用join方法。我没有得到任何错误 样本: public class JoinItself extends Thread { public void run() { System.out.println("Inside the run method "); System.out.println(Thread.currentThread().isAlive()); for(int i

我很怀疑,当一根线连在一起时会发生什么。i、 e线程自己调用join方法。我没有得到任何错误

样本:

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 ...");

    }

}
public类本身扩展了线程{
公开募捐{
System.out.println(“内部运行方法”);
System.out.println(Thread.currentThread().isAlive());

对于(inti=0;i而言,线程连接自身的概念没有意义

碰巧,
join()
方法使用
isAlive()
方法 确定何时从
join()方法返回
不检查线程是否正在连接自身。
换句话说,
join()
永远等待

我应该得到任何错误吗

我不希望出现错误。for
Thread.join()
不会说这是一个错误,可以想象,一些疯子可能会用它作为另一种方法来进行
睡眠,所以一个未记录的错误将是一个坏主意


我猜Sun不认为这是一个值得特别注意的情况。

连接等待来自另一个线程的通知。在这种情况下,同一个线程正在等待自己通知,因此没有收到通知。程序将永远不会结束。

我们可以检查join()源代码:

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);
}
public final void join()引发InterruptedException{
加入(0);
}
公共最终同步的无效联接(长毫秒)
抛出中断异常{
长基=System.currentTimeMillis();
long now=0;
如果(毫秒<0){
抛出新的IllegalArgumentException(“超时值为负”);
}
如果(毫秒==0){
while(isAlive()){
等待(0);
}
}否则{
while(isAlive()){
长延迟=毫秒-现在;
如果(延迟999999){
抛出新的IllegalArgumentException(
“纳秒超时值超出范围”);
}
如果(纳米>=500000 | |(纳米!=0&&millis==0)){
米利斯++;
}
连接(毫秒);
}
这取决于join yourself线程和alive()方法的方式
希望这篇文章能说明问题

我最喜欢这个问题和代码的平静:Thread.currentThread().isAlive()!不能等待进程尝试获取自己的退出代码!:DWorth注意,您可以在
run()
之外调用
join()
,当
run()
完成时它将返回。在关闭方法中很有用。