Java development notes (one hundred and five) several timer thread pool

Introduced in front of the thread pool of common usage, it is most tasks, they are no special requirements for specific execution timing, at most, hope for an early finish early results. But for regular implementation of the task requires, they are required to run at a specific point in time, and often run more than once, but also periodically run repeatedly. Since the common thread pool can not meet the need for such regular operation, and therefore also provides a Java thread pool to achieve timer function timing cycles to execute the task.
Common thread pool of tools called ExecutorService, timer thread pool utility class is called the ScheduledExecutorService, add a Scheduled prefix, indicating that it is a planned, pre-arranged thread pool. Unlike divided into four categories of common thread pool thread pool timer only divided into two categories: single-threaded timer thread pool and a fixed number of timer thread pool. Wherein the single-threaded thread pool timer newSingleThreadScheduledExecutor obtained by the method, it creates the following sample code:

		// Create a timer delay a single-threaded 
		ScheduledExecutorService pool = (ScheduledExecutorService) Executors.newSingleThreadScheduledExecutor ( );

 

For a fixed number of the timer is obtained by newScheduledThreadPool thread pool method, which creates the following sample code:

		// Create a timer delay time of multi-threaded (thread pool size 3) 
		ScheduledExecutorService the pool = (ScheduledExecutorService) Executors.newScheduledThreadPool (3);

 

Although only two kinds of timer thread pool, but the timer There are three ways of scheduling as many are based mainly on the number of starts to divide and cycle length, described in detail as follows:
1, regular tasks start only once.
At this time, the calling thread pool object schedule methods, the first method is a parameter of the task instance, the second and third parameters are the length and deferred execution units.
2, a plurality of time intervals of the timing cycle is initiated task.
At this time, the calling thread pool object scheduleAtFixedRate method, the first parameter is a task instance of the method, the second parameter is the length of the first time of execution, which parameters are the third long interval for subsequent runs, compared with the fourth parameter long units.
3, a plurality of fixed delay time to start a scheduled task.
At this point the calling thread pool object scheduleWithFixedDelay method, the method described in the basic parameters of the same scheduleAtFixedRate method. That the difference between the two methods: the former time interval is calculated from the start time of the task, which is calculated from the interval end time on the task.
In addition to these three scheduling method, ScheduledExecutorService ExecutorService also has all of the methods, including getPoolSize, getActiveCount, shutdown, etc., because it was originally derived from ExecutorService come ah.

Here an experiment to look at two timers thread pool to run the process, before the start of the experiment to define a mission to visit, mainly used to print the current operation log information including hours of operation, operation thread operation description. Visit task code examples are shown below:

	// definition of a task visit 
	Private Visit static class the implements the Runnable { 
		Private String name; // name of the task 
		private int index; // number task 
		public Visit {(String name, int index) 
			this.name = name; 
			this.index index = ; 
		} 
		
		@Override 
		public void RUN () { 
			// log the following printing operation, including operation time, operation of the thread, the operation information described 
			String desc = String.format ( "No.% d% s task of a visit", name, index); 
			. PrintUtils.print (Thread.currentThread () getName (), desc); 
		} 
	};

 

Then single-threaded command calling schedule timer thread pool method of performing a task timing, specific experimental sample code is as follows:

	// testing a single-threaded delay timer 
	Private static void testSingleScheduleOnce () { 
		// create a single-threaded time delay timer 
		The ScheduledExecutorService the pool = (The ScheduledExecutorService) Executors.newSingleThreadScheduledExecutor (); 
		for (int I = 0; I <. 5 ; i ++) {// loop conducted five scheduled 
			// create a visiting mission 
			visit visit = new visit ( "single-threaded timer delay time", i); 
			// thread pool command to carry out the task scheduling. Tasks performed after Visit 1 second delay 
			pool.schedule (Visit,. 1, TimeUnit.SECONDS); 
		} 
	}

 

Run the above experiment code, the thread pool is observed following log:

15:49: 0 task 16.122 pool-1-thread-1 delay timer threaded once a visit 
of 1 16.123 pool-1-thread- 1 single-threaded timer delay time: 15:49 a visit tasks 
15: 49: 16.123 pool-1 -thread-1 delay timer of a single-threaded two tasks a visit 
15: 49: 16.124 pool-1 -thread-1 a single delay the third timer task thread a visit to 
15: 49: 16.124 pool-1 -thread-1 delay timer of a single thread of a visit four tasks

 

Seen from the log, the timer thread pool from beginning to end there is only one thread running.

Test again fixed number of timer thread pool, this time into scheduleAtFixedRate method call, prepare a fixed frequency periodic task execution timing, specific experimental sample code is as follows:

	// test fixed rate multithreading timer 
	Private static void testMultiScheduleRate () { 
		// create a fixed rate multithreading timer (thread pool size. 3) 
		The ScheduledExecutorService the pool = (The ScheduledExecutorService) Executors.newScheduledThreadPool (. 3); 
		for ( int i = 0; i <5 ; i ++) {// loop conducted five scheduled 
			// create a visiting mission 
			visit visit = new visit ( "fixed-rate multi-threaded timer", i); 
			// command to carry out the thread pool task scheduling. The first visit to delay the execution of the task after one second, three seconds after the interval of every visit perform a task 
			pool.scheduleAtFixedRate (Visit,. 1,. 3, TimeUnit.SECONDS); 
		} 
	}

 

Run the above experiment code, the thread pool is observed following log:

15:50: 0 multithreading timer task 21.859 pool-1-thread-1 of a visit to a fixed rate 
of 1 21.859 pool-1-thread- 2 fixed rate multithreading timer: 15:50 a visit tasks 
15:50: 21.859 the second task multithreading timer pool 1-thread-3-a visit to a fixed rate of 
15: 50: 21.860 pool-1 -thread-3 multiple fixed rate the third timer task thread a visit 
15:50: 4 task multithreading timer 21.861 pool-1-thread-3 to the fixed rate a visit to 
15: 50: 24.790 pool-1 -thread -3 the first task multithreading timer fixed rate a visit 
15:50: 24.791 third task multithreading timer pool-1-thread-3 to the fixed rate a visit 
15:50: the fourth task multithreading timer 24.792 pool-1-thread-3 to the fixed rate a visit to 
15: 50: 24.793 pool-1 -thread-2 fixed rate multithreading timer task this second a trip 
15:50: 0 task 24.798 pool-1-thread-1 fixed rate multithreading timer a visit

 

Seen from the log, the timer thread pool opens up a total of three threads to perform regular tasks, notes that each task before and after the log interval less than 3 seconds, just three seconds is not the end to explain spaced intervals before and after the two runs.

The method then calls into scheduleWithFixedDelay, try at regular intervals to periodically perform regular tasks would be like, specific experimental sample code is as follows:

	// test multithreaded fixed delay timer 
	Private static void testMultiScheduleDelay () { 
		// create a fixed rate multithreading timer (thread pool size. 3) 
		The ScheduledExecutorService the pool = (The ScheduledExecutorService) Executors.newScheduledThreadPool (. 3); 
		for ( int i = 0; i <5 ; i ++) {// loop conducted five scheduled 
			// create a visiting mission 
			visit visit = new visit ( "fixed delay multithreading timer", i); 
			// command to carry out the thread pool task scheduling. The first visit to delay the execution of the task after one second, after the visit every 3 seconds perform a task 
			pool.scheduleWithFixedDelay (Visit,. 1,. 3, TimeUnit.SECONDS); 
		} 
	}

 

Run the above experiment code, the thread pool is observed following log:

16:10: 0th task 19.281 pool-1-thread-1 fixed delay timer multithreaded a visit 
16:10: 19.281 first 1 pool-1-thread-2 multithreaded fixed delay timer a visit tasks 
16: 10: 19.281 pool-1 -thread-3 fixed to the second task multithreading timer delay a visit to 
16: 10: 19.283 pool-1 -thread-3 multiple fixed delay the third timer task thread a visit to 
16: 10: 19.283 pool-1 -thread-2 multithreaded fixed delay timer of a visit four tasks 
16: 10: 22.283 pool-1 -thread -1 first fixed delay timer task multithreading a visit to 
16: 10: 22.284 pool-1 -thread-2 multithreaded fixed delay timer of a visit three tasks 
16:10: the second task 22.285 pool-1-thread-3 fixed delay timer multithreaded a visit 
16:10: 4 task 22.286 pool-1-thread-3 fixed delay timer of this multithreading a trip 
16:10: 0th task 22.287 pool-1-thread-1 fixed delay timer multithreaded a visit

 

Seen from the log, the log before and after this time each task is not less than 3 seconds, the method does demonstrate scheduleWithFixedDelay taken at fixed intervals rather than a fixed rate.



See more Java technology articles " Java Development Notes (order) List of chapters "

Guess you like

Origin www.cnblogs.com/pinlantu/p/10958829.html