I will post the entire code first and then explain the program by breaking the program down into first principles.
public class Timer {
public static void wait(int num,int ms)
{
try
{
Thread.sleep(ms);
}
catch(InterruptedException ex)
{
System.out.println(ex);
}finally{
if(num == 0){ //base condition
System.out.println("Timer ended");
return;
}
System.out.println(num);
}
wait(num - 1, ms);
}
public static void main(String[] args) {
wait(10,1000);
}
}
The above program will be compiled and a 10-second timer will run on the output console. Now, let me explain the semantics as an algorithm.
Step 1: Create a method with two parameters. One parameter is the number and the other parameter is the time in milliseconds.
Step 2: The simple logic behind building a timer is printing a set of numbers with a 1-second delay in between. You can't just simply print numbers from 1-10 on the console in a second and call it a timer. It's just a program. The difference is in the delay. You have to delay a second after every number is printed.
How to do this?
It's a concept called Multithreading. A thread is the lightest sub-process in the OS. The smallest unit of processing. Processes exist in the OS and each process is made up of many threads. Threads come into play whenever a program is executed. Every main method has a thread associated with it. Multithreading is executing all threads simultaneously to achieve multitasking. You can read about multithreading here in-depth.
A thread runs and terminates whenever you print numbers from 1-10 in a millisecond. But, what if we could delay a thread? What if we could figure out a way to delay a thread by a specified number of seconds and then execute that thread at a time a user desires?
Thankfully, Java provides this luxury. Thread is a class in Java's libraries that implements a Runnable interface and contains its own set of methods. You can read more about it here. Among which one method is "sleep". You can use this method to delay a thread by a specified number of milliseconds and pass it as a parameter.
Note: You can call this method without inheriting the Thread class or implementing the Runnable interface. Since the "sleep" method is static, you can invoke it using the Thread class.
But, here's the thing, Java raises an exception(a runtime-error object)if you delay a thread. Solution? Simple. You just handle the exception by using the try-catch block.
try
{
Thread.sleep(ms);
}
catch(InterruptedException ex)
{
System.out.println(ex);
}
Here, you handle the exception by specifying "Interrupted Exception" in the brackets.
An Interrupted Exception is a runtime error object that is thrown whenever a thread is delayed/sleeping.
The above block of code will ensure that each thread will be run after a delay.
Step 3: In the finally block, you add a base condition and print the current number. I added a condition because I did not want to print the number 0. I just wanted to print "Timer ended" and exit the function.
After printing the current number, make a recursive call to the function but with the value of the number decreased by 1 and pass it as a parameter along with the same time in the "wait" method. This way the function calls itself printing numbers until 1.
finally{
if(num == 0){ //base condition
System.out.println("Timer ended");
return;
}
System.out.println(num);
}
wait(num - 1, ms); //recursive call with one number less
}
In the main method, pass the values to the "num" parameter and the "ms" parameter by calling the wait method.
public static void main(String[] args) {
wait(10,1000); //1000 Milliseconds = 1 second
}
}
So, the current number is 10, the method first
->delays a thread by 1 second.
->prints the number.
->makes a recursive call by decreasing the number.
->delays the thread by 1 second and then
->prints 9,8,.....1 in this exact way and
->exits the function when the number becomes 0.
Congratulations, you have just built your timer on Java.
I initially wrote this when my friend asked me to build a timer to count the number of seconds left for 2023. I thought it would be easy and would complete it in 30 minutes tops, but the exact opposite happened. I didn't go anywhere with the project, so I taught myself Multithreading and looked up Stack Overflow to understand interrupted exceptions and Thread class.
Happy Coding!