Java Multithreading: Thread Pools (Video Tutorial Part 5)

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.

After starting the video, click the maximise button to make it fullscreen so you can see the code!



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.");
    }


 
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.