DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2005
    Posts
    9

    how to make parent thread wait for child thread to complete

    Hi,
    From my main thread, i am spawning few child threads.
    Code:
    AnObject obj = new AnObject();
    for(int i =0;i<someInt;i++) {
              ATarget aTarget = new ATarget(obj);
              Thread t = new Thread(aTarget);
              t.start();
    }
     
    obj.waitForAllThreadsAndDoSomething();




    In the last method call, i want to do something which requires all the threads to be finished processing. One brute force approach is to implement the method waitForAllThreadsAndDoSomething() in the following manner:
    Code:
    while(true) {
        if(aCounter >= childrenThreadsCount) {
           //do something
        }
    }


    but this is too ugly and memory consuming.

    can anyone suggest a cleaner approach by using methods in thread class like: wait(), notify(), yeild() ?

    cheers

  2. #2
    Join Date
    Oct 2004
    Posts
    311
    Dirst of all, you do not want to go create objects inside a loop.
    Second, you need to have the Thread instance availlable outside of the loop to be able to call any methods on it.
    Quote Originally Posted by yogeshseth
    Code:
    AnObject obj = new AnObject();
    for(int i =0;i<someInt;i++) {
              ATarget aTarget = new ATarget(obj);
              Thread t = new Thread(aTarget);
              t.start();
    }
     
    obj.waitForAllThreadsAndDoSomething();
    should be:
    Code:
    AnObject obj = new AnObject();
    Thread[] ta = new Thread[someInt];
    for(int i =0;i<someInt;i++) {
              ATarget aTarget = new ATarget(obj);
              Thread t = new Thread(aTarget);
              t.start();
              ta[i]=t;
    }
    // now we can access the  actual threads:
    for(int i =0;i<someInt;i++) {
              ta[i].join();
    }
    the join method makes the parent thread wait on the child threads.


    In the last method call, i want to do something which requires all the threads to be finished processing. One brute force approach is to implement the method waitForAllThreadsAndDoSomething() in the following manner:
    Code:
    while(true) {
        if(aCounter >= childrenThreadsCount) {
           //do something
        }
    }


    but this is too ugly and memory consuming.

    can anyone suggest a cleaner approach by using methods in thread class like: wait(), notify(), yeild() ?

    cheers[/QUOTE]

Similar Threads

  1. Replies: 15
    Last Post: 07-28-2005, 11:14 AM
  2. Java Thread
    By Kevin Chien in forum Java
    Replies: 3
    Last Post: 09-12-2000, 05:17 PM
  3. Make Explorer parent ?
    By Hoogie in forum VB Classic
    Replies: 1
    Last Post: 06-20-2000, 01:45 PM
  4. Re: How to make DesktopWindow a parent window.
    By Vlad D. in forum Architecture and Design
    Replies: 0
    Last Post: 05-02-2000, 05:32 PM

Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links