Try This:
synchronized static void print(String threadSequence) {
SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yyyy 'at'
hh:mm:ss:SSS a");
String now;
for(int i=0;i<10;i++){
now = fmt.format(new Date());
System.out.println(threadSequence + " " + now);
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
}
}
gives:
run:
First Thread 10/27/2009 at 06:20:20:203 PM
First Thread 10/27/2009 at 06:20:20:312 PM
First Thread 10/27/2009 at 06:20:20:406 PM
First Thread 10/27/2009 at 06:20:20:515 PM
First Thread 10/27/2009 at 06:20:20:609 PM
First Thread 10/27/2009 at 06:20:20:718 PM
First Thread 10/27/2009 at 06:20:20:812 PM
First Thread 10/27/2009 at 06:20:20:921 PM
First Thread 10/27/2009 at 06:20:21:015 PM
First Thread 10/27/2009 at 06:20:21:109 PM
Second Thread 10/27/2009 at 06:20:21:218 PM
Second Thread 10/27/2009 at 06:20:21:312 PM
Second Thread 10/27/2009 at 06:20:21:421 PM
Second Thread 10/27/2009 at 06:20:21:515 PM
Second Thread 10/27/2009 at 06:20:21:625 PM
etc etc
I guess the milliseconds are not exactly right because of the calculation of
now.
DaveB
________________________________
From: yolila <[email protected]>
To: Free Java Programming Online Training Course By Sang Shin
<[email protected]>
Sent: Tuesday, October 27, 2009 6:50:41 AM
Subject: [java programming] Re: Threading (homework 1021)- Times are all the
same
Try this:
System.out.println(sdf.format(Calendar.getInstance().getTime()));
On Oct 27, 7:11 am, Bryan Herbst <[email protected]> wrote:
> Hey all, small problem with homework 1021 (threading)-
>
> I get 10 of the same timestamp for each thread. Ex-
>
> 06:02:49:972 -0500 on 10/26/2009
> 06:02:49:983 -0500 on 10/26/2009
> 06:02:49:983 -0500 on 10/26/2009
> 06:02:49:972 -0500 on 10/26/2009
> 06:02:49:983 -0500 on 10/26/2009
> 06:02:49:983 -0500 on 10/26/2009
> 06:02:49:972 -0500 on 10/26/2009
> 06:02:49:983 -0500 on 10/26/2009
> 06:02:49:983 -0500 on 10/26/2009
>
> Over and over again. Why is the timestamp not changing with each call of
> getTime() ?
>
> Relevant tidbits of code:
>
> Calendar cal = Calendar.getInstance();
> SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss:SS Z 'on'
> MM/dd/yyyy");
>
> public void run()
> {
>
> for (int i=0; i<10; i++)
> {
> System.out.println(sdf.format(cal.getTime())) ;
> try {
> Thread.sleep(100);
> } catch (InterruptedException e) {}
> }
> }
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---