In making a report of the JSR 315-servlet Specification 3.0, I realized that one of the key points in understanding the asynchronous servlet is to understand the asynchronous processing mechanism in Java first. As a result, I quickly fell into the actuator (Executor) and Actuator Services (Executorservice)-because they are a key component of the asynchronous processing of Java. In this blog I will be on this topic I have mastered the things to do a summary.
Several conceptsTask: Defined as a small, independent activity that represents a series of work initiated at a point in time, doing some activity or calculation, and then ending. In a Web server, each incoming stand-alone request satisfies this definition. In Java, a task is represented as an instance of Runnable or callable.
Threads: You can think of it as an execution instance of a task. If the task represents a series of work that needs to be done, then the thread represents the actual execution of the task. In Java, threads are represented as instances of thread.
Synchronous Processing: Occurs when a task must be completed in the main thread execution. In other words, the main program must wait for the current task to finish before it can proceed with its own process.
Asynchronous Processing: When the main thread delegates the processing of a task to a separate, isolated thread. This thread will be responsible for the processing of the task, while the live thread returns the next thing to do with the main program.
thread Pool: represents one or more threads waiting to be assigned to work. The thread pool will bring us many benefits. First, it reduces the overhead of creating and destroying threads because the threads in the pool are reused instead of being recreated every time. Second, it can control the number of active threads in the system, which reduces the server's memory and computing burden. Finally, it allows you to delegate threading to the thread pool, simplifying your program.
Here, it is necessary to point out the three important mechanisms that work-some to deal with the arrival of the task (some requests the completion of a series of work), a task to submit to the receiver, and then the actual execution of each task. The Executor framework in Java separates-commits and processes the latter two mechanisms.
The arrival of the request is usually not within the control of the program-this depends on the request from the customer. The submission of a request is usually done: The requested task is added to the queue that enters the task, while the processing is implemented as a thread that allocates an incoming task to an idle wait in the thread pool to process.
Java 5.0 and thread pool Java 5.0 introduced their own thread pooling implementations-the Executor and Executorservice interfaces. This makes it easier for you to use the thread pool in your own programs.
Executor provides a convenient abstraction for the application's consideration of tasks. There is no need to consider the thread aspect, the application now simply handles the Runnable instance and passes it on to a Executor to handle.
The Executorservice interface inherits a very simplified Executor interface, which adds some life-cycle methods to manage threads in the thread pool. For example, you can turn off all the threads in the pool.
In addition, Executor allows you to submit a simple task to a thread in the pool to execute, Executorservice also allows you to submit a collection of tasks, or to obtain a Futrure object to track the execution of the task.
The Runnable and Callableexecutor frameworks represent a series of tasks that use Runnable or callable instances. The Runnable Run () method limit is that it has neither a return value nor a throw checked exception. Callable is a reinforced version that defines a call () method to allow some computed values to be returned, or even to throw an exception if needed.
Control your task you can get the details of a task by using the Futuretask class, which can wrap an instance of callable or Runnable. You can get a Futuretask instance by calling the return value of a Executorservice's Submit () method, or you can manually wrap your task to a futuretask before calling the Execute () method.
An instance of Futuretask because it implements the future interface through which you can monitor an executing task, cancel the task, get its execution results (as if the call () method of callable has a return value).
The most common Executorservice implementation of Threadpoolexecutor is threadpoolexecutor.
The task is presented to Threadpoolexecutor as an instance of Runnable, which is responsible for the actual processing, and your application does not need to be concerned about what is going on behind this abstraction.
Threadpoolexecutor is defined as follows:
- a pool of threads (defines the number of the most multithreaded and minimal threads);
- a Task Force column : This queue holds the tasks that are submitted, which are assigned to a thread in the thread pool, in turn. There are two main types of queues-bounded and unbounded. Adding a task to a bounded queue is always a successful addition, but a bounded queue (such as a fixed-capacity linkedblockingqueue) rejects new task additions when the suspended task reaches its maximum capacity.
- A processor that defines how to handle a rejected task (Saturation strategy): When a task cannot be added to the queue, the thread pool will call its registered reject handler to determine what will happen. The default deny policy is to simply throw a rejectedexecutionexception run-time exception, and the program catches the exception and processes it. There are other strategies, such as discardpolicy, that silently discard tasks without any notice.
- a thread factory : By default, a new thread constructed by the Threadpoolexecutor executor will have specific properties-such as thread priority, and a thread name that is determined by the number of thread pools and the number of threads in the threading pool. You can use a custom factory to override these default values.
Algorithm 1 using the actuator. Create an executor you first need to create an instance of Executor or executorservice in a global environment (such as the context of an application under a servlet container).
The executors class provides a lot of static factory methods for creating a executorservice. For example, Newfixedthreadpool () returns a Threadpoolexecutor instance with unbounded and fixed threads; the Newcachedthreadpool () method returns an unbounded queue and an unbounded number of Threadpoolexecutor instance. For the latter, if there are idle threads, the thread is reused, and if there is no idle thread, a new thread is created and added to the thread pool. Threads that have been idle for more than a fixed time will be moved out of the thread pool.
private static final Executor Executor = Executors.newfixedthreadpool (10);
If you do not use these handy methods, you may find it more appropriate to use your own defined Threadpoolexecutor-the many constructs that use it.
private static final Executor Executor = new Threadpoolexecutor (ten, 50000L, timeunit.milliseconds, new Linkedbloc Kingqueue<runnable> (100));
This constructor creates a bounded queue of size 100, a threadpoolexecutor with a fixed size of 10 for the thread pool.
2. Create one or more tasks you need to create one or more tasks that are performed by Runnable or callable instances.
3. Submit a task to an actuator once you have a executorservice, you can submit the task to it by using the Submit () or execute () method, and then a free thread from the thread pool to dequeue and execute the task.
4. Executive Task executor is responsible for managing the execution of tasks in addition to managing the thread pool and queues. What exactly happens here depends on the size limit of the thread pool, the number of idle threads, and the queue boundaries.
Typically, if the thread pool has fewer threads than its defined minimum number of threads, the new thread is created to handle the tasks in the queue until that number limit is reached.
If the number of threads in the pool exceeds the configured minimum number of threads, the thread pool will no longer create more threads, and the task will be placed in the task queue until a thread is idle to process it. If the queue is full, you have to start a new thread to handle the new task.
If the number of threads in the pool reaches the maximum number of threads configured, the thread pool will no longer start new threads, so the newly submitted task is either added to the task queue or rejected because the queue is full.
Threads in the thread pool continuously monitor the queue to get the task to execute. Threads that have waited for the maximum idle time to be configured will be terminated.
5. During shutdown of the actuator, we turn it off by invoking the shutdown () method of the actuator. You can choose whether the windfall is off or gracefully closed.
Resources
- Goetz waiting for "Java Concurrency Programming Practice"
- Oaks and Wong "Java thread"
Original link: http://www.softwareengineeringsolutions.com/blogs/2010/07/21/implementing-thread-pools-using-java-executors/.
Implementing a thread pool using the Java executor