Loading Posts...

Thread Pools In Java Example | Java Thread Pools Tutorial

Thread Pools In Java Example | Java Thread Pools Tutorial

Thread Pools In Java Example | Java Thread Pools Tutorial is today’s topic. It is an exciting topic of Java-related to multithreading that is to perform multiple tasks at the same time. Database and web server like Server Programs execute requests from various clients every day and the approach through which we can build new thread whenever a request arrives. But things have a drawback, and its disadvantage is that at every request our server spends too much time and resources also used and as there are too many threads which are actively running this will directly result in memory out of reach. So to overcome this problem, we have to limit the number of threads which are actively running.

Thread Pools In Java

See the following figure.

 

Thread Pools In Java Example

Now the question in your mind is what thread pools are?

Thread pools are the ones who use the previous threads to perform current tasks through this we can solve our problem of resource thrashing and memory wasting.

It also saves the time of thread creation and hence in this way the response is very nice 

Threads are mapped to system-level- threads in Java which are directly refer to operating system resources.

Executors, Executor and executor service

Java provides an executor framework centered around the Executor interface. Its sub-interface that is executor services and the class names thread pool executor, implementing both the interfaces.

With the help of executor, we can handle the running object and send them to the executor to execute these things.

They don’t focus on the thread mechanics, but the main focus is on the task performance by the threads. The threads which will run are running sequentially not in a random manner.

ThreadPoolExecutor

Since Java 5, a Java concurrency API provides the mechanism Executor framework. This is around the Executor interface, its a sub-interface ExecutorService, and a ThreadPoolExecutor class that implements both of the interfaces.

ThreadPoolExecutor separates a task creation and its execution. With the ThreadPoolExecutor, you only have to implement the Runnable objects and send them to an executor. It is responsible for their execution, instantiation, and running with the necessary threads.

It goes beyond that and improves performance using the pool of threads. When you send the task to the executor, it tries to use the pooled thread for the execution of the task, to avoid continuous spawning of threads.

Methods of Executor thread Pool

#newFixedThreadPool(int)

A fixed-size thread pool has been generated.

#newCachedThreadPool()

Create a thread pool that creates a new thread.

#newSingleThreadExecutor()

When available. Create a single thread.

In a fixed thread pool, if the threads are not free and the tasks are pending, then they stand in a queue and wait for their turn.

Risks generated in ThreadPool

  1. Deadlock: It occurs in multithreading, and thread pool introduces another case of deadlock, one case is the threads waiting in the queue due to the unavailability of threads.
  2. Thread Leakage: Thread Leakage occurs when the threads are removed from the pool so that the task can be performed, but in that procedure, if the thread does not return to the pool this will cause thread leakage.
  3. The thrashing of resources: As we know if there are more demands in comparison to the possible things then there is starvation problem same occur with the thread if there are more threads in comparison to the actual optimize level then there is the problem of starvation.

Now you are thinking about what will be the size of the thread pool

The answer is that it depends on your processor and the nature of the tasks you are giving to your processor. For N-type processor system, we can generally take maximum efficiency is of N or N+1 thread pool. It is a fantastic application which we can use to handle our server.

See the following programming example.

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

class Thread {
  public static void main(String[] args) {
    ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
    Runnable task = () -> {
      System.out.println("Executing Task At " + System.nanoTime());
    };

    System.out.println("Submitting task at " + System.nanoTime() + " to be executed after 10 seconds.");
    scheduledExecutorService.schedule(task, 10, TimeUnit.SECONDS);

    scheduledExecutorService.shutdown();
  }
}

See the output.

 

Java Thread Pools Tutorial

Custom thread pool implementation in Java

Java has a very robust thread pool functionality through the Executor framework. And you should not be creating your custom thread pool without an executor. I will vehemently discourage any such attempt.

Also, note that incorrect pooling or queue handling can result in deadlocks or resource thrashing as well. You can certainly avoid these problems with the Executor framework, which is well tested by the Java community.

Finally, Thread Pools In Java Example | Java Thread Pools Tutorial is over.

Recommended Posts

Deadlock in Java Example | Java Deadlock Tutorial

ConcurrentHashMap in Java Example | Java ConcurrentHashMap Tutorial

Java instanceof Operator Example | instanceof In Java

Java Operators Example | Operators In Java Tutorial

Immutable Class In Java Example

The post Thread Pools In Java Example | Java Thread Pools Tutorial appeared first on AppDividend.

Get Free Email Updates!

Signup now and receive an email once I publish new content.

I agree to have my personal information transfered to MailChimp ( more information )

I will never give away, trade or sell your email address. You can unsubscribe at any time.

user

The author didnt add any Information to his profile yet

Loading Posts...