Timing task - point systems

 1 public class Demo01 {
 2     static long count = 0;
 3     public static void main(String[] args) {
 4         Runnable runnable = new Runnable() {
 5             @Override
 6             public void run() {
 7                 while (true) {
 8                     try {
 9                         Thread.sleep(1000);
10                         count++;
11                         System.out.println(count);
12                     } catch (Exception e) {
13                         // TODO: handle exception
14                     }
15                 }
16             }
17         };
18         Thread thread = new Thread(runnable);
19         thread.start();
20     }
21 }
Thread Thread regular tasks
1  / * 
2  * implemented using class TimerTask timer task
 . 3  * / 
. 4  public  class Demo02 {
 . 5      static  Long COUNT = 0 ;
 . 6  
. 7      public  static  void main (String [] args) {
 . 8          TimerTask the timerTask = new new TimerTask () {
 . 9  
10              @Override
 . 11              public  void RUN () {
 12 is                  COUNT ++ ;
 13 is                  System.out.println (COUNT);
 14              }
 15          };
16         Timer timer = new Timer();
17         // 天数
18         long delay = 0;
19         // 秒数
20         long period = 1000;
21         timer.scheduleAtFixedRate(timerTask, delay, period);
22     }
23 
24 }
TimerTask
. 1  public  class Demo003 {
 2      public  static  void main (String [] args) {
 . 3          the Runnable Runnable = new new the Runnable () {
 . 4              public  void RUN () {
 . 5                  // Task goes here Wallpaper to RUN 
. 6                  System.out.println ( "the Hello !! " );
 . 7              }
 . 8          };
 . 9          the ScheduledExecutorService-Service = Executors.newSingleThreadScheduledExecutor ();
 10          // the second parameter is the first execution of the delay time, the third parameter is the timing of execution interval 
11         service.scheduleAtFixedRate(runnable, 1, 1, TimeUnit.SECONDS);
12     }
13 }
Timing ScheduledExecutorService thread pool

 Quartz regular tasks

1, the introduction of the jar package

 1 <dependencies>
 2         <!-- quartz -->
 3         <dependency>
 4             <groupId>org.quartz-scheduler</groupId>
 5             <artifactId>quartz</artifactId>
 6             <version>2.2.1</version>
 7         </dependency>
 8         <dependency>
 9             <groupId>org.quartz-scheduler</groupId>
10             <artifactId>quartz-jobs</artifactId>
11             <version>2.2.1</version>
12         </dependency>
13     </dependencies>
Pom.xml
1 public class MyJob implements Job {
2     public void execute(JobExecutionContext context) throws JobExecutionException {
3         System.out.println("quartz MyJob date:" + new Date().getTime());
4     }
5 }
Regular tasks
1   // 1. Create Scheduler plant 
2        the SchedulerFactory SF = new new the StdSchedulerFactory ();
 . 3        // 2. obtaining from the factory in Example scheduler 
. 4        Scheduler Scheduler = sf.getScheduler ();
 . 5  
. 6  
. 7        // 3. Create the JobDetail 
. 8        JB = JobBuilder.newJob the JobDetail (MyJob. class )
 . 9                .withDescription ( "Job the this iS a RAM") // Job description 
10                .withIdentity ( "ramJob", "ramGroup") // Job name and Group 
. 11                .build ();
 12 is  
13 is        //Task run time, SimpleSchedle effective type flip-flop 
14        Long Time = System.currentTimeMillis (). 3 * + 1000L; // 3 seconds after the start task 
15        a Date statTime = new new a Date (Time);
 16  
. 17        // 4. Create the Trigger
 18 is            // use SimpleScheduleBuilder or CronScheduleBuilder 
. 19        the Trigger T = TriggerBuilder.newTrigger ()
 20 is                    .withDescription ( "" )
 21 is                    .withIdentity ( "ramTrigger", "ramTriggerGroup" )
 22 is                    // .withSchedule (SimpleScheduleBuilder.simpleSchedule ()) 
23 is                    .startAt ( statTime)  // default the current time to start 
24-                    .withSchedule (CronScheduleBuilder.cronSchedule ( "0/2 * * * *?")) // two seconds to perform a 
25                    .build ();
 26  
27        // 5. Registration tasks and timer 
28        scheduler.scheduleJob (JB, T);
 29  
30        // 6. The start scheduler 
31 is        scheduler.start ();
Start scheduling classes

Expression cron shown: http://cron.qqe2.com/

Guess you like

Origin www.cnblogs.com/yuhuiqing/p/11785209.html