Java Executors create a fixed-size thread executor

Introduction

Executors class has a method to create a fixed-size thread executor.

This executor has a maximum number of threads.

If we send more tasks than the number of threads, the executor won't create additional threads.

The remaining tasks will be blocked until the executor has a free thread.

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

class Task implements Runnable {

  private Date initDate;

  private String name;

  public Task(String name) {
    initDate = new Date();
    this.name = name;
  }//w  ww.  j  a  v a  2 s .  c  om

  @Override
  public void run() {
    System.out.println(Thread.currentThread().getName()+" "+ name+" "+  initDate);
    System.out.println(Thread.currentThread().getName()+" "+ name+" "+  new Date());

    try {
      Long duration = (long) (Math.random() * 10);
      System.out.println(Thread.currentThread().getName()+" "+  name+" "+ duration);
      TimeUnit.SECONDS.sleep(duration);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    System.out.println(Thread.currentThread().getName()+" "+  new Date()+" "+  name);
  }

}

class Server {

  private ThreadPoolExecutor executor;

  public Server() {
    executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(5);
  }

  public void executeTask(Task task) {
    System.out.println("new task has arrived\n");
    executor.execute(task);
    System.out.println("Pool Size:"+" "+  executor.getPoolSize());
    System.out.println("Active Count:"+" "+  executor.getActiveCount());
    System.out.println("Task Count:"+" "+  executor.getTaskCount());
    System.out.println("Completed Tasks:"+" "+  executor.getCompletedTaskCount());
  }

  public void endServer() {
    executor.shutdown();
  }
}

public class Main {
  public static void main(String[] args) {
    Server server = new Server();
    for (int i = 0; i < 100; i++) {
      Task task = new Task("Task " + i);
      server.executeTask(task);
    }
    server.endServer();
  }

}



PreviousNext

Related