How to know if other threads have finished?

前端 未结 12 1915
野性不改
野性不改 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:40

    I would suggest looking at the javadoc for Thread class.

    You have multiple mechanisms for thread manipulation.

    • Your main thread could join() the three threads serially, and would then not proceed until all three are done.

    • Poll the thread state of the spawned threads at intervals.

    • Put all of the spawned threads into a separate ThreadGroup and poll the activeCount() on the ThreadGroup and wait for it to get to 0.

    • Setup a custom callback or listener type of interface for inter-thread communication.

    I'm sure there are plenty of other ways I'm still missing.

提交回复
热议问题