Question: need help

I have a task count of 250.
I want all these tasks to be done via a fixed no of threads. ex 10
so, I want to feed each thread one task

```
t1 =1
t2=2
.
.
.

```

currently I have written the code in such a way that, among those 10 threads each thread picks one task.
and I will wait till all the 10 threads are done using the `thread.join()`.and then again create 10 threads and feed each thread one task.

### Problem:

Although some of the threads are finished performing the task. I need to still wait for other threads to complete.
I want to maintain the thread count as 10 through . That is if among 10 threads if one thread finished the task, it can pick another task and similarly others.

any solution?

### my Code:

```java

for(int i=0;i<finData.size();i++){
      ArrayList<Thread> AllThreads = new ArrayList<>();

      if(i+10 <= finData.size()){
          p=i+10;
      }else{
          p=i+10-finData.size();                            
      } 

     for(int k=i;k<p;k++){
        FeeConfigCheck = new  Thread(new FeeConfigCheck(finData.get(k)));
        AllThreads.add(FeeConfigCheck);
        FeeConfigCheck.start(); 
        Thread.sleep(100);                          
     }  

     for(int h=0;h<AllThreads.size();h++){
        AllThreads.get(h).join();
     }
     if(p<10){
            System.out.println("Pointer----------------"+i);
            break;
     }
     i=p;
     System.out.println("Pointer----------------"+i);
}

```

Answer: 
You're reinventing the wheel; this is not neccessary. Java already contains functionality to have a fixed number of threads plus a number of tasks, and then having this fixed number of threads process all the tasks: [Executors](https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/concurrent/Executors.html)

```
ExecutorService pool = Executors.newFixedThreadPool(10);
for (int k = i; k < p; k++) {
    Runnable r = new FooConfigCheck(finData.get(k));
    pool.submit(r);
}
pool.shutdown();
while (!pool.awaitTermination(1000, TimeUnit.SECONDS)) ;

```

That's all you have to do here.

See the javadoc of these classes for the various things you can ask it; how many tasks are in the queue is something you can ask, if you need that information.

EDIT: Added a call to shutdown.
```
ExecutorService pool = Executors.newFixedThreadPool(20);
int count=0;
while(count<finData.size()){                        
 if (pool instanceof ThreadPoolExecutor) {
    if(((ThreadPoolExecutor) pool).getActiveCount()<20){
        System.out.println("Pointer -----"+count);
        Runnable r = new FeeConfigCheck(finData.get(count));
        pool.submit(r);
        count++;
        Thread.sleep(500);
     }
  }
}
pool.shutdown();
while (!pool.awaitTermination(1000, TimeUnit.SECONDS)) ;

```

This Worked for me. The pointer was keep on incrementing even when all threads are busy. So i used the getActiveCount() to know the number of active threads. this way i was able to feed the thread whenever it completed its task.Thanks all for the help. :)