Seeking Active Threads

Asked

Viewed 614 times

4

I have the following problem, where I have one for() that opens 17 threads and needs to recover, in another thread ( 18ª ) the active threads that I opened earlier and verify which of the 17 are still active. I took a look through the internet, but I couldn’t find any plausible solution to my problem.

What would be the best solution to recover the active threads I opened earlier?

Follow the Code:

for(int i = 1; i <= 500; i+=30){
  Runnable r = new Async(empresa,i,30);
  Thread t = new Thread(r);             
  t.start();
}
  • There’s a way you can add how you’re doing this(start the 17 threads)?

  • 1

    Could add the treads to an array

  • 1

    Possible solution: http://stackoverflow.com/a/26540115/5524514

  • Thank you William and Diego.

1 answer

5


Well, I found the solution by assigning each thread created to a List array, where I later check if it is active, taking each thread from the list.

The Code was like this:

List<Thread> threadArray = new ArrayList<Thread>();

for(int i = 1; i <= 500; i+=30){
    Runnable r = new Async(empresa,i,30);
    Thread t = new Thread(r);               
    threadArray.add(t);
    t.start();      
}

Later on the other Thread:

for (Thread t : threadArray) {                  
    System.out.println("Ativo: "+t.isAlive());
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.