Saturday, September 6, 2014

5. Thread Pools

Java Multithreading: Thread Pools
A tutorial on how to manage multiple threads in Java using thread pools. With thread pools you can assign a whole gaggle of threads to work through your queue of tasks.

Code For This Tutorial
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

class Processor implements Runnable {

      private int id;

      public Processor(int id) {
            this.id = id;
      }

      public void run() {
            System.out.println("Starting: " + id);

            try {
                  Thread.sleep(5000);
            } catch (InterruptedException e) {
            }

            System.out.println("Completed: " + id);
      }
}

public class App {

      public static void main(String[] args) {

            ExecutorService executor = Executors.newFixedThreadPool(2);

            for (int i = 0; i < 5; i++) {
                  executor.submit(new Processor(i));
            }

            executor.shutdown();

            System.out.println("All tasks submitted.");

            try {
                  executor.awaitTermination(1, TimeUnit.DAYS);
            } catch (InterruptedException e) {
            }

            System.out.println("All tasks completed.");
      }
}            
Output:
All tasks submitted.
Starting: 1
Starting: 0
Completed: 1
Starting: 2
Completed: 0
Starting: 3
Completed: 2
Starting: 4
Completed: 3
Completed: 4
All tasks completed.

More Information


No comments:

Post a Comment