0

Basically I have implemented this simple code:

public static void main( String[] args ){
    int it = 1;
    ExecutorService executorService = Executors.newFixedThreadPool(2);

    while(true){

        for(int i = 0; i < 500; i++){
            System.out.println("Exec " + it + " " + i);
            executorService.execute(new Runnable(){
                @Override
                public void run(){
                    System.out.println("entered");
                    try{
                       Thread.sleep(1000);
                    }catch ( InterruptedException e ){
                            e.printStackTrace();
                    }
               }
            });
            }
            System.out.println("Finished it "+ it++);
        }
    }

The code above starts adding infinite tasks to the executorService and does not wait to have threads available. This means that we can be at iteration N and still be executing task from iteration 1...

I want to be able to execute the tasks but I want to serve them as threads in the pool are available so i won't run out of resources.

Thank you.

Vml11
  • 361
  • 1
  • 2
  • 11

0 Answers0