Scheduledexecutorservice class Schedulewithfixeddelay () and schedulefixedrate () difference

Source: Internet
Author: User

First say Schedulewithfixeddelay (),

Schedulewithfixeddelay is literally understood to be a fixed delay (time) to perform the thread task, which is actually the execution time of the pipeline task, each time after the task has been completed and then delayed a fixed time before the next time.

And Schedulefixedrate, is to perform a thread task at a fixed frequency, the meaning of fixed frequency is that may set a fixed time is not enough to complete the thread task, but it does not matter, to achieve the set delay time will be executed next time.

I don't know if you understand, but here's an example:

public static void Schedulewithfixeddelay () {

	final scheduledexecutorservice Scheduledexecutorservice = Executors.newscheduledthreadpool (2);
	Bell thread
	final Runnable beeper = new Runnable () {public
		void run () {
			SimpleDateFormat SF = new Simpledateforma T ("Yyyy-mm-dd HH:mm:ss");
			Long time = (long) (Math.random () * 1000);
			The name of the output thread and the time System.out.println to use the target object and Hibernate
			(Sf.format (New Date ()) + Thread: +thread.currentthread (). GetName () + ": Sleeping "+time+" "MS");
			try {
				thread.sleep (time);
			} catch (Interruptedexception e) {
			}
			}
		};
		Set execution thread schedule, initial 10s delay, delay 10s after each task is completed and perform another task
		final scheduledfuture<?> sfuture= Scheduledexecutorservice.schedulewithfixeddelay (beeper,10,10,timeunit.seconds);

		Cancel Thread Task
		scheduledexecutorservice.schedule (New Runnable () {public
			void run () {sfuture.cancel) after 40s
				(true );
				Scheduledexecutorservice.shutdown ();
			}
		, timeunit.seconds);

	


Execution results:

2013-10-16 10:45:51 Thread: pool-1-thread-2:sleeping 726ms
2013-10-16 10:46:02 Thread: pool-1-thread-2:sleeping 288ms
2013-10-16 10:46:12 Thread: pool-1-thread-2:sleeping 294ms

Only 3 executions were performed from the number of execution results, because each time the task was performed and then the next time, the 40s 10s delay was insufficient to perform 4 times.

public static void Scheduleatfixedrate () {//Declaration thread pool final Scheduledexecutorservice Scheduledexecutorservice = executors.
	Newscheduledthreadpool (1); The bell thread final Runnable beeper = new Runnable () {public void run () {SimpleDateFormat SF = new SimpleDateFormat ("yyy
			Y-mm-dd HH:mm:ss ");
			Long time = (long) (Math.random () * 1000); The name of the output thread and the time System.out.println to use the target object and Hibernate (Sf.format (New Date ()) + "Thread:" + thread.currentthread (). GetName () + ": Sleepi
			Ng "+ Time +" MS ");
			try {thread.sleep (time);
	The catch (Interruptedexception e) {}}}; Plan the bell, the initial delay of 10s then with the 10s frequency to perform the bell final scheduledfuture<?> Beeperhandle =

	Scheduledexecutorservice.scheduleatfixedrate (Beeper, ten, timeunit.seconds); Cancel the bell and close the thread final Runnable cancelbeeper = new Runnable () {public void run () {System.out.println (thread.currentthr
			EAD (). GetName () + "Cancel ...");
			Beeperhandle.cancel (TRUE);
		Scheduledexecutorservice.shutdown ();
	}
	}; 60s after execution scheduleatfixedrate ScheduledeXecutorservice.schedule (Cancelbeeper, timeunit.seconds); }

Execution results:

2013-10-16 10:16:50 Thread: pool-1-thread-1:sleeping 868ms
2013-10-16 10:17:00 Thread: pool-1-thread-1:sleeping 587ms
2013-10-16 10:17:10 Thread: pool-1-thread-1:sleeping 313ms
2013-10-16 10:17:20 Thread: pool-1-thread-1:sleeping 969ms
Pool-1-thread-1cancel ...

Look at the time we can know is every 10s to execute the next time.

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.