[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Thread and program flow control

Posted on 2000-05-15
4
Medium Priority
?
167 Views
Last Modified: 2010-04-01
/*
I have a program that starts a bunch of threads after init.  The main program will pause its execution, and waits for the threads to finish.  Now, I can only think of a pooling loop to syn the program flow.  Is there a better way to avoid this busy pooling loop?
*/

public class ThreadTest {
  public static void main(String[] args) {
 
    Thread[] dummies = new Thread[100];
    for(int i=0; i<100; i++)
      dummies[i] = new Thread(new DummyThread());

    for(int i=0; i<100; i++)
      dummies[i].start();

//== Is there a better way to replace this pooling loop? ==
    boolean done = false;
    while(!done) {
      Thread.yield();
      done = true;
      for(int i=0; i<100; i++) {      
        if(dummies[i].isAlive()) {
        done = false;      
        break;
      }
      }       
    }
//=========================================================
   
    System.out.println("Done");
  }
}

class DummyThread implements Runnable {
  private static int threadCount=0;
  private int threadID=++threadCount;

  public void run() {
    for(int i=0; i<10; i++) {
      System.out.println ("Thread ID:" + threadID + "\t" + i);
      Thread.yield();
    }
  }
}
0
Comment
Question by:joey9394
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 2811253
One way or another, I believe that you'll have to check to see if the threads are still alive.  If you know that each thread is going be terminated smoothly (ie. it won't be killed by another thread) then you can have an array of booleans.  Each thread would initialize it's member in the array to true when the thread starts, and would set it to false when it finished.  This way you wouldn't have to poll the threads, but would be able to poll the array instead.  Make this array an instance variable so that all threads can access it.
 
Good luck!

Laminamia :)
0
 
LVL 16

Accepted Solution

by:
imladris earned 200 total points
ID: 2811324
An alternative to a polling loop would be to use the join method. This method will block the caller until the thread finishes. So then you would get:

for(i=0; i<100; ++i)dummies[i].join();


0
 

Author Comment

by:joey9394
ID: 2811627
imladris,

Thanks a lot!!  

By the way, in my DummyThread class, do I need to put the threadID=++threadCount in a synchornized method, or it is ok now?

Many thanks.
0
 
LVL 16

Expert Comment

by:imladris
ID: 2811718
Yes. Access to any "global" variable needs to be guarded by synchronized access.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question