Posts

Showing posts with the label multi-threading

Countdown Timer In Java Using Threading

Image
This is a simple Countdown program in Java which is done using threading. In this program the console is periodically cleared with a Console Object. The Basic design is to make the thread sleep for 1 sec till the give time is reached. The hours and Mins along with seconds are at first converted to seconds and a loop does the rest of the work ! Screenshot [Demo] The Code: import java.io.Console; import java.util.Scanner; public class timer { public static final char ESC = 27; public static void main(String as[])throws Exception { Scanner s=new Scanner(System.in); String tim; int hh,mm=0,ss=0; int sec; Console c = System.console(); if (c == null) { System.err.println("no console"); System.exit(1); } System.out.println("Enter In this format: \"HH:MM:SS\""); tim=s.next(); hh=Integer.parseInt(tim.substring(0,2)); mm=Integer.parseInt(tim.substring(3,5)); ss=Integer.parseInt(tim.substring(6,8)); sec=hh*3600+mm*60+ss; ...

Delayed Printing Using Threads In Java

Image
The bellow code snippet will perform the printing of the numbers in the loop till with a time gap of 1sec. You can modify the bellow snippet and add your creativity to try something new. Basically Thread.sleep()  makes the thread to wait for the specified time ! Screenshots[Demo]: The Code: class DelayPrint { public static void main(String args[]) { try { for(int n = 5; n > 0; n--) { System.out.println(n); Thread.sleep(1000);// Time Gap of 1 sec } } catch (InterruptedException e) { System.out.println("Main thread interrupted"); } } } Got Bugs, Drop them here ! And yes don't forget to subscribe us ! ;)