-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimerTaskClass.java
More file actions
34 lines (31 loc) · 1.12 KB
/
TimerTaskClass.java
File metadata and controls
34 lines (31 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.Timer;
import java.util.TimerTask;
import java.util.Scanner;
public class TimerTaskClass {
public static void main(String[]args)
{
//Timer=Class that schedules tasks at specific time or periodically
// useful for: sending notifications,scheduled updates,repetitive actions
//TimerTask=Represents the task that will be executed by the timer
// you will extend TimerTask class to define your task
// create a subclass of TimerTask and @override run
Scanner sc=new Scanner(System.in);
System.out.print("Enter the # seconds to countdown from: ");
int count=sc.nextInt();
Timer timer=new Timer();
TimerTask task=new TimerTask(){
int j=count;
@Override
public void run()
{
System.out.println(j);
if(j==0){
System.out.print("HAPPY BIRTHDAY");
timer.cancel();
}
j--;
}
};
timer.schedule(task,2000,1000);
}
}