How to know if other threads have finished?

前端 未结 12 1716
野性不改
野性不改 2020-11-22 14:05

I have an object with a method named StartDownload(), that starts three threads.

How do I get a notification when each thread has finished executing?

12条回答
  •  长发绾君心
    2020-11-22 14:21

    You can interrogate the thread instance with getState() which returns an instance of Thread.State enumeration with one of the following values:

    *  NEW
      A thread that has not yet started is in this state.
    * RUNNABLE
      A thread executing in the Java virtual machine is in this state.
    * BLOCKED
      A thread that is blocked waiting for a monitor lock is in this state.
    * WAITING
      A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
    * TIMED_WAITING
      A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
    * TERMINATED
      A thread that has exited is in this state.
    

    However I think it would be a better design to have a master thread which waits for the 3 children to finish, the master would then continue execution when the other 3 have finished.

提交回复
热议问题