Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

runnable interface in java

public class CountDown {
	// A Java code that creates 5 parallel tasks
	// performing countdown from 5 to 0
	public static void main(String[] args) {
		CountDown application = new CountDown();
		application.start();	
	}
	// Method for spawning 5 parallel threads of execution
	public void start() {
		for(int i=1; i<=5; i++)
			new Thread(new CountDownHandler(5, i)).start();
	}
	// Utility class for handling count down per thread
	// that implements the Runnable interface
	private class CountDownHandler implements Runnable {
		private int id;
		private int counter;
		public CountDownHandler(int counter, int id) {
			this.counter = counter;
			this.id = id;
		}
		public String status() {
			return "id#" + id + " " +
			((counter >= 0) ? counter : "LiftOff!");
		}
		public void run() {
			while(counter >= -1) {
				System.out.println(status());
				counter--;
				try {
					Thread.sleep(1000);
				} catch(Exception e) {

				}
			}
		}
	}
}
Comment

PREVIOUS NEXT
Code Example
Java :: java convert float to int 
Java :: public String toString() { 
Java :: Java Scanner nextLine() 
Java :: change number into array in java 
Java :: math.sin java 
Java :: check whether a double is not a number in java 
Java :: java setbounds 
Java :: remove fragment from backstack 
Java :: empty array java 
Java :: jframe in java 
Java :: check if number is odd java 
Java :: how to print hello world java 
Java :: upload converted base64 to image object to oss without saving in java 
Java :: define a list java 
Java :: java to check if its a number scanner 
Java :: android list index 
Java :: what method is use for getting the index position of a character of a string in java 
Java :: how to get last element of array java 
Java :: from string to double java 
Java :: java remote debug 
Java :: all possible valid IP addresses leetcode 
Java :: pretty print json in console 
Java :: split number java 
Java :: Right triangle star pattern in java 
Java :: how to get child from layout in android 
Java :: joining an array of strings in java 
Java :: java switch case 
Java :: java create unmodifiable list 
Java :: how to right align in java 
Java :: java instant from timestamp string 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =