CodeByAkram: Thread
Showing posts with label Thread. Show all posts
Showing posts with label Thread. Show all posts

Multi threading using Executor Services in Java

 The ExecutorService interface, executes tasks in parallel in background and represents an asynchronous execution mechanism. The ExecutorService create and maintain the reusable thread pool.


How to create ExecutorService?

To create ExecutorService, you can use Executors factory to create the instance of ExecutorService. Lets see some examples.


 There are diffrent way to execute or submit the task with ExecutorService.
  1. submit(Runnable)
  2. submit(Callable)
  3. execute(Runnable)
  4. invokeAny(...)
  5. invokeAll(...)
Lets go through with the example to run multiple treads with ExecutorService.

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

public class HTMLUnitMultipleThreads {

	public static void main(String[] args) {

		int threadSize = 5;
		ExecutorService executor = Executors.newFixedThreadPool(threadSize);
		for (int i = 0; i < threadSize; i++) {
			executor.submit(new MultiThreadingService());
		}
	}
}

public class MultiThreadingService implements Runnable {

	@Override
	public void run() {
		for (int i = 0; i < 10; i++) {
			System.out.println("Thread Name = " + Thread.currentThread().getName());
		}

	}

}

What is Thread in Java

What is Thread in Java?


Java Thread is an independent path of execution within a program which can run in parallel with other existing Threads.

Now lets talk about Process.

A process is a self contained execution environment and it can be seen as a program. However a program itself contains multiple processes inside it. Java runtime environment runs as a single process which contains different classes and programs as processes.

So we can say that, thread can be light weight process because it require less resources and also multiple threads can share the same resource. Java provides built-in support for multi-threaded programming.

Let’s summarize  in points:

1. The main purpose of multi-threading is to provide simultaneous execution of two or more parts of a program. Each such part of a program called thread.

2. Threads are lightweight processes, they share the common memory space. 

3. In Multi threaded environment, programs that are benefited from multi-threading, utilize the maximum CPU time so that the idle time can be kept to minimum.

Now lets talk about the state of a thread. Thread can be in one of the following states:-

NEW – A thread that has not yet started is in this state.
RUNNABLE – A thread executing in the Java virtual machine is in this state.
BLOCKED – A thread that is blocked waiting for a monitor lock is in this state.
WAITING – A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
TIMED_WAITING – A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
TERMINATED – A thread that has exited is in this state.
A thread can be in only one state at a given point in time.








Simple Deadlock Explanation

Deadlock Explanation with Example.


Deadlock means, it is a situation where 2 or more threads are blocked and waiting for each other.

Let's take an example, in the office we have shared printer and scanner where employees has ability to do scanning and printing.

1. Deepak has bunch of documents that it wants to print first and also want to take a scan later.
(Print and Scan)
2. Rohit has bunch of documents that it wants to scan first and also want to take a print later.
(Scan and Print)

Thread 1 ------ (Got Printer) ------ (Need to Scan) (Blocked)
Thread 2 -------(Got Scanner) -----(Need to Print) (Blocked)