Master java skills

Thread Pool / Group

A thread pool is a pool of threads which are worker threads. Meaning, when a task is assigned, one of the threads from the pool takes that up, does it and returns back in the pool. When multiple tasks are assigned to the pool, then multiple threads take those up, complete them and go back in the pool.

The advantage of using thread pool is that thread creation and managment efforts are reduced, specially when we need to create theads more frequently.

We will see here one simple example of thread pool using executor framework. Later, we will discuss Executor framework in detail.

Thread pool example

package com.javatrainingschool;

public class RunnableTask implements Runnable {
	
	public void run() {
		for(int i=0; i < 3; i++) {
			System.out.println(Thread.currentThread().getName() + " : Value of i = " + i);
		}
	}
}
package com.javatrainingschool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPoolExample {

	public static void main(String[] args) {
		
		ExecutorService executor = Executors.newFixedThreadPool(4);
		for(int i=0; i<6; i++) {
			RunnableTask task = new RunnableTask();
			executor.execute(task);
		}
		executor.shutdown();
	}
}
Output :
pool-1-thread-2 : Value of i = 0
pool-1-thread-2 : Value of i = 1
pool-1-thread-4 : Value of i = 0
pool-1-thread-3 : Value of i = 0
pool-1-thread-1 : Value of i = 0
pool-1-thread-3 : Value of i = 1
pool-1-thread-3 : Value of i = 2
pool-1-thread-4 : Value of i = 1
pool-1-thread-2 : Value of i = 2
pool-1-thread-3 : Value of i = 0
pool-1-thread-4 : Value of i = 2
pool-1-thread-1 : Value of i = 1
pool-1-thread-3 : Value of i = 1
pool-1-thread-2 : Value of i = 0
pool-1-thread-3 : Value of i = 2
pool-1-thread-1 : Value of i = 2
pool-1-thread-2 : Value of i = 1
pool-1-thread-2 : Value of i = 2

Thread Group

java.lang.ThreadGroup is a class that can be used to group a set of threads into a single object. Using which, we can take some common actions on them.

Let’s understand it by one example.

In the below example, we are doing an empty implementation of Runnable task because we don’t have to print anything from here. We only have to print information for ThreadGroup class.

package com.javatrainingschool;

public class RunnableTask implements Runnable {
	
	public void run() {
		//empty implementation
	}
}
package com.javatrainingschool;

public class ThreadGroupExample {

	public static void main(String[] args) {
		
		ThreadGroup tgrp = new ThreadGroup("My Thread Group");
		
                RunnableTask task = new RunnableTask();
		Thread t1 = new Thread(tgrp, task, "Thread1");
		Thread t2 = new Thread(tgrp, task, "Thread2");
		Thread t3 = new Thread(tgrp, task, "Thread3");
		
		t1.start();
		t2.start();
		t3.start();
		
		tgrp.list();
	
	}	
}
Output :
java.lang.ThreadGroup[name=My Thread Group,maxpri=10]
    Thread[Thread1,5,My Thread Group]
    Thread[Thread2,5,My Thread Group]
    Thread[Thread3,5,My Thread Group]