NOTES: Java Concurrency Practice 2

Source: Internet
Author: User

 Public Interface Executor {    void  execute (Runnable command);}

Although executor is a simple interface, it provides the foundation for a flexible and powerful asynchronous task framework that supports a variety of different types of task execution strategies. It provides a standard way to decouple the submission process of a task from the execution process and to represent the task with runable. The implementation of executor also provides support for the lifecycle, as well as mechanisms such as statistical information collection, application management mechanisms, and performance testing.

Executor is based in the Producer-consumer pattern, where activities that submit tasks are the producers (Produci ng units of work to is done) and the threads, that execute tasks is the consumers (consuming those units of work).

Application Server in the program:

classTaskexecutionwebserver {Private Static Final intNthreads = 100; Private Static FinalExecutor exec =Executors.newfixedthreadpool (nthreads); Private Static FinalExecutor EXEC1 =NewThreadpertaskexecutor (); Private Static FinalExecutor EXEC2 =NewWithinthreadexecutor ();  Public Static voidMain (string[] args)throwsIOException {serversocket socket=NewServerSocket (80);  while(true) {            FinalSocket connection =socket.accept (); Runnable Task=NewRunnable () { Public voidrun () {//HandleRequest (connection);                }            };        Exec.execute (Task); }    }}classThreadpertaskexecutorImplementsExecutor { Public voidExecute (Runnable r) {NewThread (R). Start (); };}classWithinthreadexecutorImplementsExecutor { Public voidExecute (Runnable r) {R.run (); };}

where exec, the thread is executed as a thread pool. EXEC1, each connection is executed as a thread. EXEC2, executes the thread as a synchronous mechanism.

Execution policies is a resource management tool, and the optimal policy depends on the available computing resources and Your quality-of-service requirements. By limiting the number of concurrent tasks, you can ensure that the application does not fail due to resource exhaustion O R suffer performance problems due to contention for scarce resources. [3] separating the specification of execution policy from task submission makes it practical to select an execution PO Licy at deployment time that's matched to the available hardware.

NOTES: Java Concurrency Practice 2

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.