how to decrease a value every 20 sec [closed]

You can do this by using thread let me give you an example

import java.util.*;

public class TimerThreadTest {
   public static void main(String []args) {
      Task t1 = new Task("Task 1");
      Task t2 = new Task("Task 2");
      Timer t = new Timer();
      t.schedule(t1, 10000); //  executes for every 10 seconds
      t.schedule(t2, 1000, 2000); // executes for every 2 seconds
   }
}
class Task extends TimerTask {
   private String name;
   public Task(String name) {
       this.name = name;
   }
   public void run() {
      System.out.println("[" + new Date() + "] " + name + ": task executed!");
   }
}

in the previous example we gave two tasks and determine the time to start the second , we should got an output like this

[Thu Aug 01 21:32:44 IST 2019] Task 2: task executed!
[Thu Aug 01 21:32:46 IST 2019] Task 2: task executed!
[Thu Aug 01 21:32:48 IST 2019] Task 2: task executed!
[Thu Aug 01 21:32:50 IST 2019] Task 2: task executed!
[Thu Aug 01 21:32:52 IST 2019] Task 2: task executed!
[Thu Aug 01 21:32:53 IST 2019] Task 1: task executed!
[Thu Aug 01 21:32:54 IST 2019] Task 2: task executed!
[Thu Aug 01 21:32:56 IST 2019] Task 2: task executed!
[Thu Aug 01 21:32:58 IST 2019] Task 2: task executed!
[Thu Aug 01 21:33:00 IST 2019] Task 2: task executed!

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top