java Timer timer
Simple example code:
public class Test { public static void main(String[] args) { // Timer timer Timer mTimer = new Timer(); MyTack myTack = new MyTack(); mTimer.schedule(myTack, 2000, 3000); //The first parameter is the task that needs to be executed. The second parameter is how much time it delays the initial execution. The third parameter is how many times it takes to execute again after execution is a periodic Scanner mScanner = new Scanner(System.in); String exti = ""; while(!exti.equals("1")){ System.out.println("---->>"); exti = mScanner.next(); } System.out.println("Close"); mTimer.cancel();//Close this timer mScanner.close(); } static class MyTack extends TimerTask{ @Override public void run() { System.out.println("Execute task"); } }}Timer in thread pool
public class Test { public static void main(String[] args) { // Timer ScheduledExecutorService service = Executors.newScheduledThreadPool(3); service.scheduleWithFixedDelay(new MyRunnable(), 0, 10000, TimeUnit.MILLISECONDS); // One parameter is how long it takes to execute after delay, the third parameter is how long it needs to wait after execution after execution. The second time is periodic. The fourth parameter is calculated by type (milliseconds, seconds, minutes, etc.). }}//You need to write a class that implements the runnable interface public class MyRunnable implements Runnable { @Override public void run() { int index = 0; while (index++ < 100) { System.out.println(Thread.currentThread().getName()+" "+index); try { Thread.sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}Thank you for reading, I hope it can help you. Thank you for your support for this site!