Tuesday, August 14, 2007

Never Multi-thread Your System Clock 4

MAYBE THE FINAL SOLUTION

Threads. Threads. Threads. Do you need a real nice and cool loan for a house from your best bank? Have to check your credit score first, right? Well, for this you just have to do no checking. A trip to Hawaii or to New Zealand or to Malaysia or anywhere in the world. Anywhere in the world. Is it for free? Yes, but for your time. How? Right from the labs of google comes, Google Maps. It's just absolutely wonderful. I fell in love with google maps from the word go. Go anywhere. Let's see. Why not try the city of London?
Yeah, the final solution. Back from that travel, you can use just one thread and get as much time in any time zone that you so desire. The code below diverts off the piggy-backing threads class on these aspects: I removed the constructors for time zone threads and used a single thread in the run method. Then formatted the time instance in a single method, zoned(). Therefore, within this method, I can and you can create as many time zone instances as you like.



package overall;

import java.text.DateFormat;
import java.util.Date;
import java.util.SimpleTimeZone;

public class SingleThread implements Runnable {

public void run() {
//the limit on time
final long LIMIT = 1000*60*5;
//guard on time spent doing this shit
long timeSpent = 0;
//present time guard
Date dater = new Date();
//we'll catch an exception on this computation
try{
while(timeSpent < LIMIT){
//call the time formatter
zoned();
//sleep three second
Thread.sleep(1000*3);
//update the system time
Date now = new Date();
//compute time difference between start and now
timeSpent = now.getTime() - dater.getTime();
}
}
//catch any exception please
catch(Exception e){
e.printStackTrace();
}
}

protected void zoned(){
//an hour of time
long oneHour = 1000*60*60;
//get the date formatter
DateFormat formatter = DateFormat.getTimeInstance();
//declare the time zone. set initially to utc
SimpleTimeZone simp = new SimpleTimeZone((int)oneHour*0, "UTC");
//set the timezone of our dates to utc
formatter.setTimeZone(simp);
//print out the utc time at this moment in time
System.out.println("Time in UTC (London) is: "+ formatter.format(new Date()));
//set the time zone to hawaii time
simp.setID("HST");
simp.setRawOffset((int)oneHour*10);
formatter.setTimeZone(simp);
System.out.println("Time in HST (Hawaii) is: "+formatter.format(new Date()));
//set time zone new zealand time
simp.setID("NST"); //new zealand standard time
simp.setRawOffset(-(int)oneHour*12);
formatter.setTimeZone(simp);
System.out.println("Time in NST (Fiji) is: "+formatter.format(new Date()));
}

public static void main(String[] args){
//declare an instance of our time
SingleThread instar = new SingleThread();
//call its thread
Thread t = new Thread(instar);
//start the thread
t.start();
}
}

When I was young, we piggy-backed to get into the movie theater. This is how: A buys the ticket for the film, enters theater, the innocent one, wraps ticket on his pen, runs to the first floor of the building and throws it to B. Yeah, the ticket's already marked but the film house has a toilet that is at the outside. If B has a good tongue and can give and take shit, B can enter and so can C, and so can D, and so... The timing just has to be right. Piggy-backing and fond memories...!