Java Concurrency Programming Combat: The sixth chapter----task execution

Source: Internet
Author: User

task: usually some abstract and discrete unit of work. Most concurrent applications are constructed around "task execution" , and the work of the program is assigned to multiple tasks, simplifying the organization structure of the program to facilitate maintenance

I. Perform tasks in the thread

Task Independence: tasks do not depend on the state, results, and edge effects of other tasks. Standalone tasks can be implemented in parallel

1, serial execution of tasks

All tasks are executed serially in a single thread, with simple procedures, high security, no synchronization and so on, and the drawbacks are obvious, unable to improve throughput and responsiveness, are suitable for a small number of tasks and take a long time to execute, or only for a single user, and the user only makes one request at a time.

2. Create thread to display

Creating a thread for each request, separating the processing of the task from the main thread, multiple tasks can be processed in parallel, taking full advantage of system resources, increasing throughput and corresponding speed, requiring the processing code to be thread-safe

3, unlimited creation of the lack of threads

The cost of the thread life cycle is very high, too many threads consume system resources, the memory space of the idle thread is occupied, and a large number of threads are competing for the CPU for additional performance overhead; Stability: Breaking these limits the underlying operating system's restrictions on threading are likely to throw OutOfMemoryError exceptions

Summary: In a certain range, increasing the number of threads can help increase throughput, but a lot more may result in performance degradation.

Second, executor framework

A task is a set of logical units, and a thread is the mechanism by which the task executes asynchronously

    • Executor simplifies the management of threads, and Java.util.concurrent provides a flexible thread pool as part of the executor framework.
    • Executor based on the producer-consumer design pattern, the operating unit that submits the task is equivalent to the producer (generating the work unit to be completed) and the thread that performs the task is the consumer (after executing these units of work)
    • Decouple the commit process from the execution process and use runnable to represent the execution of the task

1. Executor-based Web server

1 public class Threadpertaskwebserver {2     private static final int nthreads = 3     /** 4      * Create thread pool with fixed number of threads 5
   */6     private static final Executor exec =  7             executors.newfixedthreadpool (nthreads); 8 public     static void Main (string[] args) throws IOException {9         serversocket server = new ServerSocket (+),         boolean listening = True;11         while (listening) {             final Socket connection = server.accept ();//blocking waits for Client connection request             Runnable task = New Runnable () {                 @Override15 public                 void Run () {                     handlerrequest (connection);                 }18             };             Exec.execute (Task),         }21         server.close ();        24}

Executor created a thread pool with 100 threads to handle tasks

If you want to change the way tasks are handled, you only need to use the executor implementation

2. Execution strategy

    • Develop a reasonable execution strategy based on available resources and requirements for quality of service
    • Decoupling task submissions from task execution to help you select the execution strategy that best matches your hardware during the deployment phase

3. Thread pool: Manages a pool of resources for a set of homogeneous worker threads.

Thread pool vs Task column: Worker threads come from thread pool, get tasks from work queue, execute back to thread pool

Pros: Not only can the huge overhead of thread creation and destruction be apportioned when multiple requests are processed, but the other benefit is that when a request arrives, the worker thread usually already exists, so the execution of the task is not delayed by waiting for the thread to be created

    • Newfixedthreadpool: fixed-length thread pool, that is, the size of the thread pool is capped.
    • Newcachedthreadpool: A cacheable thread pool that recycles idle threads if the current size of the thread pool exceeds the processing requirements, and when the demand increases, you can add new threads, noting that there is no limit to the size of the thread pool.
    • Newsinglethreadexecutor: Single-threaded executor, which executes a serial task by creating a single worker thread, executor creates another thread instead if the thread ends abnormally. Note this pattern ensures that tasks are executed serially in the order in which they are queued (for example, FIFO, LIFO, priority).
    • Newscheduledthreadpool: Creates a fixed-length thread pool and performs tasks in a deferred or timed manner.

4. Executor life cycle

Newxxxthreadpool are all returned executorservice.

The Executorservice life cycle is dominated by three states: running, shutting down, and terminating.

To address the life cycle of service execution, Executorservice extends the executor interface and adds a way to manage the lifecycle.

    • Shutdown: Closes the thread pool, no longer accepts new tasks, waits for the task that has been submitted to complete
    • Shutdownnow: Forces an immediate shutdown of the thread pool, returns a list of waiting tasks to execute, throws an interrupt exception in the execution of the task
    • IsShutDown: Whether it is in the closed state
    • Isterminated: Whether to end
    • awaitterminated: Blocking waits for shutdown to complete

5. Delay tasks and cycle tasks

Use Scheduledthreadpoolexecutor to replace Timer,timertask.

    • The timer is based on absolute time, scheduledthreadpoolexecutor based on relative time.
    • The timer performs all scheduled tasks to create only one thread, which can easily break the timing accuracy of other timertask if it takes too long to execute.
    • The timer does not catch an exception, Timetask throws an unchecked exception that terminates the timer thread, the timertask that have been dispatched but not executed will no longer be executed, the new task will not be dispatched, and a "thread leak" occurs
1 public class Outoftime {2 public     static void Main (string[] args) throws Interruptedexception {3         Timer timer = New Timer (); 4         Timer.schedule (new Throwtask (), 1);//The first task throws an exception of 5         Thread.Sleep (+);  6         Timer.schedule (New Throwtask (), 1);//The second task will no longer execute, and throw an exception to the timer already cancelled. 7         Thread.Sleep (5000); 8         System.out.println ("end."); 9     } Ten     static class Throwtask extends timertask{12 @Override14 public         void Run () {             New RuntimeException ("Test timer ' s error behaviour");         }17     }18}

Third, find out the parallelism that can be exploited

1. The task of carrying the result callable and future

Runnable flaw: Cannot return a value, or throw an exception

Callable and Runnable both describe abstract computational tasks, callable can return a value, and can throw an exception

Executor 4 lifecycles for performing tasks: Create, submit, start, complete. In the executor framework, you can cancel a task that has been committed but not started and, for tasks that have already started, can only be canceled if they can respond to an outage, and canceling a task that has already been completed has no effect.

The future represents the life cycle of a task, providing a way to determine whether it is complete or canceled and to get the results of the execution

    • Get method: If the task is completed, return the result or throw executionexception; If the task is canceled, throw cancellationexception; If the task is not completed, block wait for the result
    • Executorservice's Submit method submits a callable task and returns a future to determine the execution state and get the result of the execution
    • Secure Publishing Process: threading a task from the commit thread to the thread from the compute thread to the call to the Get method

2. The limitations of heterogeneous task parallelism: When the execution efficiency between heterogeneous tasks is very large, it is not very effective for overall performance improvement.

3. Complete service Completionservice (executor+blockingqueue)

Use Blockingqueue to save the calculation results (future), use take and poll to obtain, and calculate the same delegate to executor

4. Set a time limit for the task: if the expected execution time is exceeded, the result will not be

Summary: Designing an application around task execution simplifies the development process and helps to implement concurrency. The Executor framework decouples task submissions from execution policies and supports several different types of execution strategies. When you need to create threads to perform tasks, consider using executor. To maximize the benefits of decomposing an application into different tasks, you must define a clear task boundary. There are obvious task boundaries in some applications, while further analysis is needed in other programs to reveal finer granularity of parallelism.

Summary of methods

The Get, Cancel, iscancelled, Isdone methods of the future

get: Blocks until the task is complete. Throws three kinds of exceptions: cancellationexception-If the calculation is canceled, executionexception-if the calculation throws an exception, interruptedexception-if the current thread is interrupted while waiting.

get (long timeout, timeunit unit): blocks until the time-out and the task is not completed. In addition to throwing the above three kinds of exceptions

Cancel (Boolean mayinterruptifrunning): An attempt was made to cancel execution of this task. If the task is completed, canceled, or cannot be canceled for some other reason, the attempt will fail. When Cancel is called, if the call succeeds and the task has not been started, the task will never run. If the task is already started, the mayinterruptifrunning parameter determines whether to invoke the interrupt action of the thread that is running the task.

iscancelled: Returns True if the task is canceled before it is properly completed

isDone: Normal termination, exception, or cancellation, in all of these cases, this method will return true
Executorservice's Submit, InvokeAll and Invokeany methods

Executorservice has three overloads of the Submit method:

1, can receive runnable or callable type of task, return future<?> type of future get return NULL.
2, these three methods will be submitted to the task of converting to the future implementation class Futuretask instance, and as a return instance of submit.
3, another call to these three methods will not block, not like InvokeAll to wait until all tasks are completed before returning, and unlike Invokeany, wait until a task is completed before returning to the future.
4. This three method calls executor's execute to complete, because Executor's execute throws rejectedexecutionexception-if this task is not accepted, NullPointerException -If the command is null these two run into the exception, so these three methods will also throw these two exceptions.

T Invokeany (collection<callable<t>> tasks):
1, as long as a task has completed successfully (that is, the exception is not thrown, this is not the same as the task Completion concept: Task completion is to specify the future of Isdone return True, it is possible to throw an exception after the completion state), only return this result. Once a normal or an exception is returned, the unfinished task is canceled (that is, the thread that the task is running to handle the interrupt state, and the interrupt exception will be thrown once there is an interruptible blocking method call on it).
2. This method blocks until a task is completed (normal or abnormal exit).
3, also call Executor's execute to complete
4. Call get does not block

Invokeany (collection<callable<t>> tasks, long timeout, timeunit unit):
1. The result is returned as long as a task has completed successfully before the given timeout expires (that is, the Invokeany method cannot throw an exception, including the exception thrown by Future.get). Once normal or abnormal returns, the unfinished task is canceled.
2. This method blocks until a task is completed (normal or abnormal exit).
3, also call Executor's execute to complete
4. Call get does not block

list<future<t>> InvokeAll (collection<callable<t>> tasks):
1. Only when all tasks are completed will the future list of hold task status and results be returned. Returns the Future.isdone () of all elements of the list to true. Note that the task can be completed normally or by throwing an exception.
2. This method blocks until all tasks are completed (normal or abnormal exit).
3, is also called Executor's execute to complete, if the task executes the process throws the other exception, then the method exits unexpectedly, and cancels all other unfinished tasks that have not yet been performed.
4. The future of the returned list is a completed task, and the get will no longer block

InvokeAll (collection<callable<t>> tasks, long timeout, timeunit unit):
1. When all tasks are complete or expire (whichever occurs first), return to the future list that holds the status and results of the task (if the list is returned in a time-out, the list includes those tasks that have not yet been completed. Cancellationexception exceptions may be thrown when getting results using get). Returns the Future.isdone () of all elements of the list to true. Once returned, the unfinished task is canceled. Note that you can complete the task either normally or by throwing an exception.
2. This method blocks until all tasks are completed (normal completion or abnormal exit or timeout).
3, is also called Executor's execute to complete, if the task executes the process throws the other exception, then the method exits unexpectedly, and cancels all other unfinished tasks that have not yet been performed.
4. The future of the returned list will have a task that is not completed when the task is timed out, and the get will throw cancellationexception or executionexception, of course, and all of them will not block.

Java Concurrency Programming Combat: The sixth chapter----task execution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.