Executor to start the line turndown with the thread start () better

Source: Internet
Author: User

Java5 Why the executor thread pool framework was introducedDisadvantages of 1.new Thread ()

(1) every new Thread () consumes performance
(2) calling new Thread () creates a lack of management of threads, called wild threads, and can be created indefinitely, competing with each other, leading to excessive use of system resources and paralysis of the system
(3) Not conducive to expansion, such as scheduled execution, regular execution

2. Advantages of using thread pool

(1) Reuse of existing threads, reduce the cost of object creation, destruction, performance is good
(2) Can effectively control the maximum number of concurrent threads, improve the utilization of system resources, while avoiding excessive resource competition, to avoid clogging
(3) Provide the functions of timing execution, regular execution, single thread, concurrency control, etc.

Java provides 4 thread pools via executor1.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. Terminate and remove from cache examples of threads that have not been used for 60 seconds:
Executorservice executor = Executors.newcachedthreadpool (); for(inti =0; I <Ten; i++) {Runnable task =NewRunnable () {@Override          Public void Run() {System.out.println (Thread.CurrentThread (). GetName ());     }     }; Executor.execute (Task);}

Run results: You can see that the cache thread pool size is indeterminate, you may need to create a different number of threads, when using a cached pool, see if there are any previously created threads in the pool, and if so, reuse them. If not, a new thread is added to the pool, and the cache pool is typically used to perform some short-lived asynchronous tasks.

2.newFixedThreadPool creates a thread pool of a fixed number of threads that can control the maximum number of concurrent threads, and the excess thread waits for an example in the queue:
Executorservice executor = Executors.newfixedthreadpool (2); for(inti =0; I <Ten; i++) {Runnable task =NewRunnable () {@Override          Public void Run() {System.out.println (Thread.CurrentThread (). GetName ());     }     }; Executor.execute (Task);}

Run Result: A total of only 2 threads are created, starting to execute 2 threads, when 2 threads are active, the resubmitted task joins the queue until the other threads run the end, and the next task is reused when the thread is idle.

3.newScheduledThreadPool creates a thread pool that supports timed and recurring task executions, which in most cases can be used as an alternative to the Timer class example:
Executorservice executor = Executors.newscheduledthreadpool (2); for(inti =0; I <Ten; i++) {Runnable task =NewRunnable () {@Override          Public void Run() {System.out.println (Thread.CurrentThread (). GetName ());     }     }; Executor.schedule(task, 3, TimeUnit.SECONDS);}

Run Result: similar to Newfixedthreadpool, the difference is that the newscheduledthreadpool is not executed until the specified time is delayed.

4.newSingleThreadExecutor creates a single threaded thread pool that performs tasks with only a single worker thread, ensuring that all tasks perform the example in the specified order (FIFO, LIFO, priority):
Executorservice executor = Executors.newscheduledthreadpool (); for(inti =0; I <Ten; i++) {Runnable task =NewRunnable () {@Override          Public void Run() {System.out.println (Thread.CurrentThread (). GetName ());     }     }; Executor.execute(task);}

Operation Result: Creates only one thread, and then executes the second after the last one has finished executing.

&NBSP;

executorservice:

same point:bothsubmit and Execute are Executorservice methods, all of which are added threads to the thread pool

different points:Submit has return value return to future, execute no, future can execute Cancle method (Cancel execution ), can be determined by the Get () method, whether the execution succeeds (null means execution succeeds)

Executor to start the line turndown with the thread start () better

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.