How to use the concept of thread pool in Java?

I am creating an HTTP proxy in java. I have a class called Handler that is responsible for handling requests and responses coming and coming from a web browser and a web server, respectively. I have another class called Copy that copies the inputStream object to the outputStream object. Both of these classes implement the Runnable interface. I would like to use the concept of a thread pool in my design, however I do not know how to do it! Any hints or ideas would be greatly appreciated.

+4
source share
2 answers

I suggest you take a look at the Contractor and the Contractor. They add a lot of good things to make it easier to use thread pools.

...

@ Azad provided good information and links. You should also buy and read the Java Concurrency book in practice. (often abbreviated as JCiP) Note for stackoverflow of big wigs - what about some profitable Amazon link ???

The following is my brief summary on how to use and use ExecutorService with thread pools. Let's say you want 8 threads in a pool.

You can create it using the full-featured ThreadPoolExecutor constructors, for example.

ExecutorService service = new ThreadPoolExecutor(8,8, more args here...); 

or you can use simpler but less customizable Executors factories, for example.

 ExecutorService service = Executors.newFixedThreadPool(8); 

One of the benefits you get right away is the ability to shutdown() or shutdownNow() the thread pool and check this status with isShutdown() or isTerminated() .

If you don't care about the Runnable that you want to run, or they are very well written, standalone, never interrupted or log errors properly, etc .... you can call

 execute(Runnable r); 

If you care about any result (say, it calculates pi or loads an image from a web page) and / or bothers you if there was an exception, you should use one of the submit methods that Future returns. This will allow you at some point in the future to check if there is an isDone() task and get the result via get() . If there was an exception, get() will throw it (wrapped in an ExecutionException). Note. Even your future does not return anything (this is a type of Void), it may be good practice to call get() (ignoring the result of void) to check for an exception.

However, this test of the Future is a bit of a problem with the chicken and the egg. The whole point of the thread pool is to send tasks without blocking. But the Future.get () and Future.isDone () blocks ask questions about which thread calls it, and what if it is not done, do you sleep () and block?

If you simultaneously send a known part of related tasks, for example, you perform a large mathematical calculation, for example, matrix multiplication, which can be performed in parallel, and there are no particular advantages for obtaining partial results, you can call invokeAll() . Then the calling thread is blocked until all tasks are completed, when you can call Future.get() for all futures.

What if tasks are more scattered or do you really want to use partial results? Use the ExecutorCompletionService that wraps the ExecutorService. At the end of the job, they are added to the queue. This makes it easier for a single thread to poll and remove events from the queue. JCiP has a great example web page application that downloads all images in parallel and displays them as soon as they become available to respond.

+10
source

I hope the below helps you :,

class Executor
object that executes the submitted Runnable tasks. This interface provides a way to decouple the task assignment from the mechanics of how each task will be performed, including information about thread consumption, scheduling, etc. Typically, Executor is used instead of explicitly creating threads . For example, instead of calling new Thread(new(RunnableTask())).start() for each of the many tasks you can use:

 Executor executor = anExecutor; executor.execute(new RunnableTask1()); executor.execute(new RunnableTask2()); ... 


class ScheduledThreadPoolExecutor
A ThreadPoolExecutor , which can optionally schedule the execution of commands after a specified delay or execute them periodically. This class is preferable to Timer when several workflows are needed or when the additional flexibility or capabilities of ThreadPoolExecutor (which is distributed throughout this class) are required.

Delayed tasks are completed no sooner than they are resolved, but without any real-time guarantees about when they will start after they are turned on. Tasks scheduled for the same run time are included in the first-in-first-out (FIFO) send order.

and
Interface ExecutorService
Executor , which provides methods for managing termination and methods that can create the future for tracking the progress of one or more asynchronous tasks.

An ExecutorService can be disabled, which will stop accepting new tasks. After completing the work, the contractor will eventually stop working, and at that moment no tasks will be actively performed, no tasks are waiting for execution, and no new tasks can be submitted.

Edited by:
you can find an example using Executor and ExecutorService
here
here
and here
The question will be helpful to you.

+4
source

Source: https://habr.com/ru/post/1389775/


All Articles