如果其他线程正在等待,该线程是否隐式调用notifyAll()? [英] Does a thread implicitly call notifyAll() , if other Threads are waiting on it?

查看:87
本文介绍了如果其他线程正在等待,该线程是否隐式调用notifyAll()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我应该说的第一件事是,我不是在寻找解决方案,这是hwk,但是它运行正常,澄清一下对我有很大帮助.

First thing i think i should say is that i am not looking for a solution , this is hwk , but it runs correctly , what would help me greatly is come clarification..

我们只是在面向对象的编程类中介绍了线程,并且收到了一个分配,我已经完成了.在我的代码中,我从不调用notifyAll(),但是似乎在退出run()之前隐式调用了它.我的另一个同学也有同样的问题.我在某处读到,正在等待另一个线程的线程死亡/退出run方法时,该线程被通知(隐式notifyAll()?).这是我的代码(runnable,以及所有代码的主要运行位置)

We were just introduce to threads in my object oriented programing class , and received an assignment , which I completed. In my code I never call notifyAll() , but it seems as though it is called implicitly before exiting the run(). Another classmate of mine had the same question. I read somewhere that a Thread that is waiting on another Thread is notified ( implicit notifyAll() ?) when the thread it is waiting on dies/exits the run method. here is my code ( the runnable , and the main where it all runs)

如果我删除等待,并用Throwable替换InterruptedException,则代码运行,但未正确运行,线程在其睡眠时间后出现,然后打印终止消息并按出现的顺序消亡.

if I remove the wait, and the replace the InterruptedException with Throwable , the code runs , but not correctly , the threads appear after their sleep time then print the termination message and die in the order they appear.

我无法发布图片,所以请尽我所能描述它的工作原理: 每个线程都在分配的时间内休眠并保存一个Thread数组,该数组保存对它必须等待的线程的引用

I couldnt post a pic so ill do my best to describe how it works: each thread sleeps for the alloted time and holds a Thread array , which holds references to threads it must wait for

ex:T1到达2秒取决于T2,T3 T2到达4秒取决于无人 T3到达6秒取决于无人 所以... T1到达并且必须等待T2和T3才能终止,T2和T3可以立即终止.

ex: T1 arrives at 2 seconds depends T2,T3 T2 arrives at 4 seconds depends on no-one T3 arrives at 6 seconds depends on no-one so... T1 arrives and has to wait for T2 and T3 before it can terminate, T2 and T3 can terminate immediately..

以下是到达时间及其依赖对象:

here are the arrival times and who they depend on:

T1 4秒,无 T2 6秒,无人 T3 7秒,无人 T4 2秒,T1,T2 T5 3秒,T3 T6 1秒,T3,T4 T7 8秒,T4 T8 5秒,T6

T1 4 sec , no-one T2 6 sec , no-one T3 7 sec , no-one T4 2 sec , T1,T2 T5 3 sec , T3 T6 1 sec , T3,T4 T7 8 sec , T4 T8 5 sec , T6

很抱歉,我需要冗长的解释,我没有在寻找解决方案,代码无法正常运行,我需要澄清如何调用wait和notify永远如何运行?

sorry about the horrendously long explanation , i am not looking for a solution , the code runs properly , i need some clarification on how if wait is called and notify is never called how it runs?

public class TScheduler {




    public static void main(String[] args) {




    long originTime = System.currentTimeMillis();

    // Long.parse.long(args[0]) * 1000
    DepThread t1 = new DepThread(originTime , 4000 , new Thread[]{});
    Thread T1 = new Thread(t1);
    T1.setName("T1");
    // Long.parse.long(args[1]) * 1000
    DepThread t2 = new DepThread(originTime , 6000 , new Thread[]{});
    Thread T2 = new Thread(t2);
    T2.setName("T2");

    DepThread t3 = new DepThread(originTime , 7000 , new Thread[]{});
    Thread T3 = new Thread(t3);
    T3.setName("T3");

    DepThread t4 = new DepThread(originTime , 2000 , new Thread[]{T1,T2});
    Thread T4 = new Thread(t4);
    T4.setName("T4");

    DepThread t5 = new DepThread(originTime , 3000 , new Thread[]{T3});
    Thread T5 = new Thread(t5);
    T5.setName("T5");

    DepThread t6 = new DepThread(originTime , 1000 , new Thread[]{T3,T4});
    Thread T6 = new Thread(t6);
    T6.setName("T6");

    DepThread t7 = new DepThread(originTime , 8000 , new Thread[]{T4});
    Thread T7 = new Thread(t7);
    T7.setName("T7");

    DepThread t8 = new DepThread(originTime ,5000 , new Thread[]{T6});
    Thread T8 = new Thread(t8);
    T8.setName("T8");

    DepThread t9 = new DepThread(originTime , 500 , new Thread[]{T7});
    Thread T9 = new Thread(t9);
    T9.setName("T9");




    T1.start();
    T2.start();
    T3.start();
    T4.start();
    T5.start();
    T6.start();
    T7.start();
    T8.start();
    T9.start();






    }

}




public class DepThread implements Runnable {

    long sleepTime;
    Thread[] depThrdArray;
    public boolean done = false ;
    long baseTime ;

    public DepThread( long baseTime , long arrivalTime ,  Thread[] depThrdArray ){
    //super();
    this.baseTime = baseTime;
    this.sleepTime = arrivalTime;
    this.depThrdArray = depThrdArray;
    this.done = false;
    }

    @Override
    public void run() { 
    try {
        Thread.sleep(sleepTime);
        System.out.println( Thread.currentThread().getName() + " arrived at " + (System.currentTimeMillis() - baseTime));
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    for ( int i = 0 ; i < depThrdArray.length ; i ++){
        if ( depThrdArray[i].isAlive()){
        synchronized(depThrdArray[i]){
            try {
            depThrdArray[i].wait();
            System.out.println(Thread.currentThread().getName() + " waiting on " + depThrdArray[i].getName() 
                + " time " + (System.currentTimeMillis() - this.baseTime));
            } catch (InterruptedException e) {
            e.printStackTrace();
            }
        }
        }

    }

    this.done = true;
    //synchronized (this){
    //    notifyAll();
    //}
    System.out.println(Thread.currentThread().getName() + "  at time " + ( System.currentTimeMillis() -  baseTime) + " terminating");
    }

}

推荐答案

线程在终止时调用notifyAll以支持join功能. Javadoc中针对 Thread#join :

Thread calls notifyAll upon termination in support of the join functionality. This is documented in the Javadoc for Thread#join:

此实现使用以this.isAlive为条件的this.wait调用循环.当线程终止时,将调用this.notifyAll方法.建议应用程序在Thread实例上不要使用waitnotifynotifyAll.

This implementation uses a loop of this.wait calls conditioned on this.isAlive. As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances.

那么基本上有两个结论:

There are essentially two conclusions then:

  • 如果要等到线程终止,请使用join.
  • 如果要使用wait,请勿将Thread实例用作监视器.
  • If you want to wait until a Thread terminates, use join.
  • If you want to use wait otherwise, do not use a Thread instance as a monitor.

这篇关于如果其他线程正在等待,该线程是否隐式调用notifyAll()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆