Study notes: Summarize the four timer modes used.

1. Java built-in timer

Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { System.out.println("Time's up!" ); }}, 3 * 1000100);Copy the code

2. Use thread pool tools

ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(2); scheduledThreadPool.scheduleAtFixedRate(new Runnable() { @Override public void run() { System.out.println("========================="); } }, 1000, 2000, TimeUnit.MILLISECONDS); ` `scheduledThreadPool.scheduleAtFixedRate(new Runnable() { @Override public void run() { System.out.println(System.currentTimeMillis()+"<><>"+System.nanoTime()); } }, 1000, 2000, TimeUnit.MILLISECONDS);Copy the code

3. Use @enablesCheduling provided by SpringBoot

Enable @enablescheduling on Main

Annotate the method you want to run with @scheduled (initialDelay = 2000,fixedDelay = 5000)

4. Quartz timed task scheduler, which has been abandoned due to limited usage.

5. Use spirngboot timer to access the database in real time

Write statement in Mapper:

@Select("SELECT id,user_name FROM t_zhx_user ORDER BY id DESC")
List<TZhxUser> getNameLimit2();
Copy the code

Call the Mapper in the method

@Scheduled(cron = "*/15 * * * * *") public void getNameLimit2() { List<TZhxUser> nameLimit2 = tZhxUserMapper.getNameLimit2(); System. Out. Println (" -- -- -- -- -- -- -- -- start -- -- -- -- -- -- -- -- "); for (int i = 0; i <nameLimit2.size() ; i++) { System.out.println(nameLimit2.get(i)); } system.out. println("--------- end --------"); }Copy the code