Friday, November 30, 2012

Program Countdown Menggunakan Thread Pada Java


public class CountDown implements Runnable{

private static int startCount = 11;
private int countDown = --startCount;
private static int threadCount = 0;
private int threadNumber = ++threadCount;

public void run() {
System.out.println("Waktu tinggal "+countDown+" detik ( Thread nomer : "+threadNumber+" )");

if (countDown == 1) {
System.out.println("\nWaktu Habis ~ !!");
}
}

private static void doThreadCountdown() throws java.lang.InterruptedException{
for (int i = 0; i < 10; i++){
Thread.sleep(1000);
Runnable ot = new CountDown();
Thread th = new Thread(ot);
th.start();
}
}

public static void main(String[] args) throws java.lang.InterruptedException{
System.out.println("\nMenghitung mundur dalam 10 detik ...\n");
doThreadCountdown();
}
}

Hasil Outputan :

Penjelasan : Pada program ini menghasilkan 10 buah thread dengan menggunakan looping sebanyak 10 kali dan setiap thread yang tercipta dimanfaatkan untuk melakukan hitung mundur/countdown.

No comments:

http://goo.gl/BGVrJP

MY Motto

My photo
giving amenity to all visitor.

Total Pageviews