How to wait for threads to finish executing?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: How to wait for threads to finish executing?

  1. #1
    Haytham Guest

    How to wait for threads to finish executing?

    BlankI have a java program that create multiple threads. I want to wait
    inside the mothed that created these threads until all the threads are done
    executing.

    What do I need to do to accomplish this task?

    TIA,
    Haytham
    htale@hciasachs.com



  2. #2
    John Timney (MVP) Guest

    Re: How to wait for threads to finish executing?

    You probably need a seperate monitor thread, that has some synchronised
    trigger in identifying that a thread is starting and finishing, then you can
    poll that and see if any threads are not finished.

    Pseudo-code

    thread starts
    tells monitor its started (active thread count is now 1)
    active threads +=1
    thread starts
    tells monitor its started (active thread count is now two)
    active threads +=1
    thread ends
    tells monitor its finished
    active threads +-1 (active thread count is now 1)

    Regards

    John Timney (MVP)




    Haytham <htale@hciasachs.com> wrote in message
    news:3987348a@news.devx.com...
    > BlankI have a java program that create multiple threads. I want to wait
    > inside the mothed that created these threads until all the threads are

    done
    > executing.
    >
    > What do I need to do to accomplish this task?
    >
    > TIA,
    > Haytham
    > htale@hciasachs.com
    >
    >




  3. #3
    Paul Clapham Guest

    Re: How to wait for threads to finish executing?

    Try the join() method of the thread. Quote from the Javadoc:

    join()
    Waits for this thread to die.

    You will have to execute join() on each thread you want to wait for.

    Haytham <htale@hciasachs.com> wrote in message
    news:3987348a@news.devx.com...
    > BlankI have a java program that create multiple threads. I want to wait
    > inside the mothed that created these threads until all the threads are

    done
    > executing.
    >
    > What do I need to do to accomplish this task?
    >
    > TIA,
    > Haytham
    > htale@hciasachs.com
    >
    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center