Never Multi-Thread Your System Clock 3
A FAIR ENOUGH IMPLEMENTAION.
Am sure you did read the first blog on this topic? Let me remind you a point from it: any number of men can pass through a door which is supposed to allow only one man at any time if they can all piggyback into the door, provided the height of the door is sufficient to take as much piggybackers.
I can implement piggybacking for this time zone problem by creating three separate threads for each time zone, such that the first thread calls the second and the second calls the third. You can create any number of time zone references this mind. Like the Mahjong game, you can even create four. But caution! Sshhh... read to the end first. This thread calling is best done with ThreadGroups. A ThreadGroup makes it possible that where one thread gets interrupted or a parent thread gets interrupted, all the threads die together.
Now for the code. For a challenge, write a third thread that is declared in the second thread or the Hawaii standard time thread in the code below.
import java.lang.*;
import java.util.*;
import java.text.*;
public class FairEnough implements Runnable {
String dZone = "Local";
//every thread exists in a group such that
//they all die together
ThreadGroup dGroup = new ThreadGroup("myGroup");
//the time zones am going to use, simp for UTC and
//zHst for Hawaii Standard Time.
SimpleTimeZone simp = null;
SimpleTimeZone zHst = null;
//computing this long now prevents future recursive
//computing
long oneHour = 1000*60*60;
/** Creates a new instance of FairEnough */
public FairEnough(String zoner) {
//if string is utc, run utc else if hst run hst
if (zoner == "UTC"){
this.dZone = zoner;
//set the time zone to utc
simp = new SimpleTimeZone((int)oneHour*0, dZone);
//create the utc thread in a a thread group and start it
Thread utc = new Thread(dGroup, this);
//start our thread
utc.start();
//utc object now calls the hst object
FairEnough second = new FairEnough("HST");
}else{
//this here is constructor code for hst object
this.dZone = "HST";
zHst = new SimpleTimeZone((int)oneHour*10, dZone);
//create hst thread
Thread hst = new Thread(dGroup, this);
hst.start();
}
}
//the run method defines the thread itself.
public void run() {
//LIMIT means i don't want this thread to run for
//more than five minutes
long LIMIT = 1000*60*5;
//this is the guard on five minutes limit
long timeSpent = 0;
//this is the guard on timer, reference to the
//instance in time when run started, +/- nanoseconds
Date dater = new Date();
//our thread could be interrupted, so let's put
//it in a try
try{
while(timeSpent < LIMIT ){
//create date formatter formatting only time
DateFormat formatter = DateFormat.getTimeInstance();
//specify time zone to use
if(this.dZone == "UTC"){
formatter.setTimeZone(simp);
}else if(this.dZone == "HST"){
formatter.setTimeZone(zHst);
}
//print out our time
System.out.println(this.dZone +" time is "+formatter.format(new Date()));
//sleep 3 seconds
Thread.sleep(1000*3);
//update system time
Date now = new Date();
//compute the time spent doing this stuff
timeSpent = now.getTime() - dater.getTime();
}
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
//specify time zone to start with. if utc or hst.
//use upper case please!!Mine!!
FairEnough fair = new FairEnough("UTC");
}
}
Caution: I ran the code above on a 1.2 Ghz Pentium III Intel and was my puter humming all the time. Not to worry, he's just complaining to deaf ears. That goes to show you that this is not a very good candidate for implementing this concept, just a solution just made it. If you're really that bold, try getting driver magic if you're experimenting with clocks and threads first.
In my magic bag is a simple, affordable solution that you'll make use of and your puter wouldn't realise you came into its machine space. Ready for the last and final blog on this topic? I sure am.



