Java Concurrency Programming-executor framework

Source: Internet
Author: User

This article turns from http://blog.csdn.net/chenchaofuck1/article/details/51606224 thanks to the author

When we create threads in traditional multithreaded programming, we often create some runnable objects and then create corresponding thread objects to execute them, but if the program needs to execute a large number of tasks concurrently, it needs to create a thread for each task and manage it, which will affect the efficiency of the execution of the program. and creating too many threads will overload the system.

After JDK 1.5, a set of executor frameworks was adopted to solve these problems and to decompose the task creation and execution process. The framework includes implementation classes such as basic interfaces such as executor,executorservice,callable and executors,threadpoolexecutor .

To create a thread pool:

The most core class of the executor framework is threadpoolexecutor, which is the implementation class of the thread pool, created threadpoolexecutor typically created using executors Factory mode, The Executors class provides a series of factory methods for creating a first-line pool:

    • Public static Executorservice newfixedthreadpool (int nthreads) creates a thread pool of a fixed number of threads , representing the creation of up to nthreads threads. If the number of incoming tasks is greater than nthreads, a new thread is not created, but the blocking waits for the idle thread to execute.

    • Public static Executorservice Newcachedthreadpool () creates a cacheable thread pool that calls execute to reuse previously constructed threads if the threads are available. If an existing thread is not available, a new thread is created and added to the pool. Terminates and removes from the cache those threads that have not been used for 60 seconds.

    • Public static Executorservice Newsinglethreadexecutor () creates a single thread of executor.

    • Public static Scheduledexecutorservice newscheduledthreadpool (int corepoolsize) creates a thread pool that supports timed and recurring task execution. In most cases it can be used to replace the timer class.

Common methods:

    • ShutDown (): Closes the executor, allowing execution of the previously submitted task executor to execute before closing. After calling shutdown (), sending the task to executor will be rejected, throwing a rejectexecutionexception exception.

    • Shutdownnow (): Immediately shuts down the executor, prevents the wait task from starting, and attempts to stop the currently executing task. Returns a list of tasks awaiting execution.

    • IsShutDown (): Returns true after calling shutdown ().

    • Isterminated (): After calling shutdown (), and the executor finishes closing the procedure, returns True.

    • Getpoolsize (): Gets the number of threads for the current thread pool

    • Getactivecount (): Gets the number of active threads in the thread pool

    • Getcompletecount (): Gets the number of tasks completed in the thread pool.

Import java.util.concurrent.Executors; Import Java.util.concurrent.ThreadPoolExecutor;PublicClassexecutortest {PublicStaticvoid Main (string[] args) {Threadpoolexecutor executor = (threadpoolexecutor) executors.newcachedthreadpool ();for (int i =0; I <5; i++) {Executor.execute (New Task ()); } executor.shutdown ();while (!executor.isterminated ()) {System.out.printf ("Pool size:%d,active count:%d,completed task:%d\n", Executor.getpoolsize (), Executor.getactivecount (), Executor.getcompletedtaskcount ()); }}} class task implements runnable{ public void Run () {System.out.println (thread.currentthread (). GetName () + "is called"); try {thread.sleep (100);} catch (Interruptedexception e) { //TODO auto-generated catch block E.printstacktrace (); }}}

Results:

Pool-1-thread-2 is called
Pool-1-thread-4 is called
Pool-1-thread-5 is called
Pool-1-thread-3 is called
Pool-1-thread-1 is called
Pool size:5,active count:5,completed task:0
Pool size:5,active count:5,completed task:0
Pool size:5,active count:5,completed task:0
Pool size:5,active count:5,completed task:0

Java Concurrency Programming-executor Framework (RPM)

Related Article

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.