Tutorial Addendum On Java - Accoutrement

travelappsios lifestyleappsios utilitiesappsios
 31 December 18:00   






* TimerThread.java


* Absorb (c) 2002 by Dr. Yang




import java.util.*;


import java.text.*;


class TimerThread extends Cilia {


clandestine changeless final int NORMAL_CLOCK = 1;


clandestine changeless final int COUNT_DOWN = 2;


clandestine changeless final int STOP_WATCH = 3;


clandestine int type; // blazon of clock


clandestine int c_millisecond, c_second, c_minute, c_hour;


clandestine changeless int remaining_minutes = 5;


clandestine changeless int clock_interval = 100; // in milliseconds < 1000


accessible changeless abandoned main(String[] a) {


System.out.println("Normal alarm Stop watch Count-down");


TimerThread normal_clock = new TimerThread(NORMAL_CLOCK);


TimerThread count_down = new TimerThread(COUNT_DOWN);


TimerThread stop_watch = new TimerThread(STOP_WATCH);










while (count_down.isAlive()) {


System.out.print(normal_clock.getClock()+" "+


stop_watch.getClock()+" "+ count_down.getClock()+"


try {




} bolt (InterruptedException e) {












accessible TimerThread(int t) {


blazon = t;


if (type==NORMAL_CLOCK) {


GregorianCalendar c = new GregorianCalendar();


c_hour = c.get(Calendar.HOUR_OF_DAY);


c_minute = c.get(Calendar.MINUTE);


c_second = c.get(Calendar.SECOND);


c_millisecond = c.get(Calendar.MILLISECOND);


} abroad if (type==COUNT_DOWN) {


c_hour = remaining_minutes/60;


c_minute = remaining_minutes%60;


c_second = 0;


c_millisecond = 0;


} abroad {


c_hour = 0;


c_minute = 0;


c_second = 0;


c_millisecond = 0;






accessible abandoned run() {


while (!isInterrupted()) {


try {




} bolt (InterruptedException e) {


break; // the capital cilia wants this cilia to end




if (type==NORMAL_CLOCK || type==STOP_WATCH)


c_millisecond +=clock_interval;


abroad c_millisecond -= clock_interval;


if (c_millisecond>=1000) {


c_second += c_millisecond/1000;


c_millisecond = c_millisecond%1000;




if (c_second>=60) {


c_minute += c_second/60;


c_second = c_second%60;




if (c_minute>=60) {


c_hour += c_minute/60;


c_minute = c_minute%60;




if (c_millisecond<0) {




c_millisecond += 1000;




if (c_second<0) {




c_second += 60;




if (c_minute<0) {




c_minute += 60;




if (c_hour<0) {


c_hour = 0;


break; // end this cilia








clandestine Cord getClock() {


// abiding the alarm as a cord of HH:mm:ss:SSS format


GregorianCalendar c = new GregorianCalendar();










SimpleDateFormat f = new SimpleDateFormat("HH:mm:ss:SSS");


acknowledgment f.format(c.getTime());








The accoutrement in this program will abolish in the afterward sequence:



  • The count-down cilia will be the first one to terminate, if its alarm


    reaches the 0 value.



  • The capital cilia is consistently blockage the cachet of the


    count-down cilia by calling count_down.isAlive(). Already it detects


    the abortion of the count-down thread, it will then forward the abeyance


    signal to the accustomed alarm cilia by calling normal_clock.interrupt().



  • The accustomed alarm cilia is consistently blockage its abeyance cachet by


    calling isInterrupted(). Already it accustomed the abeyance arresting from the main


    thread, it will breach the loop, abolish itself.



  • After sending the abeyance arresting to the accustomed alarm signal, the main


    thread will aswell abolish itself.



  • The stop-watch cilia will be affected to abolish by the JVM, afterwards the


    main cilia terminates, because it is created by the capital thread, and marked


    as a apparition thread.





Please aswell agenda that:



  • The abeyance arresting may appear to a thread, while it is in the average of


    a sleep. If this happens, the sleep() alarm will be alternate anon with


    the InterruptionException. The cilia should abolish itself quickly.



  • The clocks apish in the accoutrement are not authentic at all. The added time


    spend on active the statements additional the sleep() account is counted. You will


    see that the alarm time from the accustomed alarm cilia is active slower than your


    real watch.



  • I am using the System.out.print() adjustment with the
    appearance to accumulate the


    output argument repeating at the band on the screen. This will create the achievement looks


    much added like a absolute clock.



  • The GregorainCalendar chic is acclimated in my program to me accepting the accepted


    hour, minute, additional and millisecond values, and formatting those ethics into


    a string. I accept added addendum on agenda classes in my additional book on JDK.





Output of one beheading of my program:



Normal alarm Stop watch Count-down


21:19:13:764 00:05:00:000 00:00:00:000





Tags: program, system, watch, minute, final, sleep, start, clock, notes, public, string, normal, second

 clock, thread, normal, second, minute, millisecond, count, watch, calendar, timerthread, private, terminate, static, sleep, interruption, signal, getclock, system, threads, gregoriancalendar, interval, string, itself, output, remaining, final, notes, public, start, program, , normal clock, count down, stop watch, private static, system out, main thread, interruption signal, clock interval, clock thread, terminate itself, static final, millisecond 1000, final int, new timerthread, private static final, count down thread, normal clock thread, normal clock interrupt, millisecond clock interval, count down isalive, type normal clock, timerthread count down, clock stop watch, normal clock stop, stop watch count, count down timerthread, timerthread normal clock, timerthread stop watch,

Share Tutorial Addendum On Java - Accoutrement:
Digg it!   Google Bookmarks   Del.icio.us   Yahoo! MyWeb   Furl  Binklist   Reddit!   Stumble Upon   Technorati   Windows Live   Bookmark

Text link code :
Hyper link code:

Also see ...

Article In : Computers & Technology  -  java