[Reprint] Java multithreaded learning-java.util.concurrent (iii) Scheduledthreadpoolexecutor

Source: Internet
Author: User

Reprinted from http://janeky.iteye.com/blog/770441

----------------------------------------------------------------------------------

6. Scheduledthreadpoolexecutor
Let's start by learning more about this class in the JDK1.5 API:

"You can schedule a command to run after a given delay, or execute a command on a regular basis." This class is better than a Timer when multiple worker threads are required, or if threadpoolexecutor is required to have additional flexibility or functionality.
Once the deferred task is enabled, it is executed, but there is no real-time guarantee as to when it is enabled and when it is executed. Enable tasks that are scheduled for the same execution time, in the first In, Out (FIFO) order that is submitted.

Although this class inherits from Threadpoolexecutor, several inherited tuning methods have no effect on this class. In particular, because it is a fixed-size pool that uses corepoolsize threads and a unbounded queue, adjusting maximumpoolsize has little effect. "

Prior to JDK1.5, we implemented timer/cycle operations through timers. But the timer has several dangers [JCIP]

The A.timer is based on absolute time. Easily affected by the system clock.
B.timer only creates a new thread to perform all the timetask. All timetask may have a related impact
C.timer does not catch timertask exceptions, but simply stops them. This will inevitably affect the execution of other timetask.

If you are using JDK1.5 or above, it is recommended to replace the timer with Scheduledthreadpoolexecutor. It basically solves the above problem. It uses the relative time, uses the thread pool to execute TimerTask, will come out TimerTask exception.

The following is a simple example to illustrate the use of scheduledthreadpoolexecutor.

We regularly throw the timer out of the ordinary
We regularly print system time from the console

The code is as follows (refer to some of the code on the Internet, here to express our thanks)
Java code
  1. Import Java.util.concurrent.ScheduledThreadPoolExecutor;
  2. Import Java.util.concurrent.TimeUnit;
  3. Public class Testscheduledthreadpoolexecutor {
  4. public static void Main (string[] args) {
  5. Scheduledthreadpoolexecutor exec=New Scheduledthreadpoolexecutor (1);
  6. Exec.scheduleatfixedrate (new Runnable () {//each time an exception is triggered
  7. @Override
  8. public Void Run () {
  9. throw new RuntimeException ();
  10. }}, +, timeunit.milliseconds);
  11. Exec.scheduleatfixedrate (new Runnable () {//print the system time every once in a while to prove that the two do not affect each other
  12. @Override
  13. public Void Run () {
  14. System.out.println (System.nanotime ());
  15. }}, +, timeunit.milliseconds);
  16. }
  17. }


Summary: It's time to change your timer to Scheduledthreadpoolexecutor.

[Reprint] Java multithreaded learning-java.util.concurrent (iii) Scheduledthreadpoolexecutor

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.