测试以下程序,输出系统当前时间~
一个工具类,分别是不同方法获取系统时间,如下public class TimeUtil {
public static void show(){
String date[]= getDate();
String time= getTime();
String now= getNow();
System.out.println(date[0]+ "-----"+ date[1]);
}
public static String getTime(){
Calendar calendar= Calendar.getInstance();
Date date= calendar.getTime();
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time= dateFormat.format(date);
return time;
}
public static String[] getDate() {
String[] value= new String[3];
SimpleDateFormat lFormat;
Date date = null;
value[0]= String.valueOf(new Date().getTime());
Calendar MyDate = Calendar.getInstance();
value[1]= String.valueOf(new Date().getTime());
MyDate.setTime(new java.util.Date());
date = MyDate.getTime();
lFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
value[2] = lFormat.format(date);
return value;
}
public static String getNow(){
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time= dateFormat.format(new Date());
return time;
}}然后我用一个线程类调用获得系统时间,线程类如下:public class TimeThread extends Thread {
private int num;
private String date[], time, now;
public TimeThread(int num){
this.num= num;
}
public void run(){
while(true){
date= TimeUtil.getDate();
time= TimeUtil.getTime();
now= TimeUtil.getNow();
TimeUtil.show();
// System.out.println("Thread"+ num+ ": "+ date[0]+ "------"+ date[1]);
System.out.println("Thread "+ num+ ": "+ date[2]+ "====="+ time+ "====="+ now);
System.out.println();
try {
Thread.sleep(new Random().nextInt(50000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}}然后重新写一个测试类,测试代码如下:public class TimeTest { /**
* @param args
*/
public static void main(String[] args) {
for(int i= 0; i< 10; i++){
TimeThread thread= new TimeThread(i);
thread.start();
// try {
// Thread.sleep(new Random().nextInt(2));
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
} }}
一个工具类,分别是不同方法获取系统时间,如下public class TimeUtil {
public static void show(){
String date[]= getDate();
String time= getTime();
String now= getNow();
System.out.println(date[0]+ "-----"+ date[1]);
}
public static String getTime(){
Calendar calendar= Calendar.getInstance();
Date date= calendar.getTime();
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time= dateFormat.format(date);
return time;
}
public static String[] getDate() {
String[] value= new String[3];
SimpleDateFormat lFormat;
Date date = null;
value[0]= String.valueOf(new Date().getTime());
Calendar MyDate = Calendar.getInstance();
value[1]= String.valueOf(new Date().getTime());
MyDate.setTime(new java.util.Date());
date = MyDate.getTime();
lFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
value[2] = lFormat.format(date);
return value;
}
public static String getNow(){
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time= dateFormat.format(new Date());
return time;
}}然后我用一个线程类调用获得系统时间,线程类如下:public class TimeThread extends Thread {
private int num;
private String date[], time, now;
public TimeThread(int num){
this.num= num;
}
public void run(){
while(true){
date= TimeUtil.getDate();
time= TimeUtil.getTime();
now= TimeUtil.getNow();
TimeUtil.show();
// System.out.println("Thread"+ num+ ": "+ date[0]+ "------"+ date[1]);
System.out.println("Thread "+ num+ ": "+ date[2]+ "====="+ time+ "====="+ now);
System.out.println();
try {
Thread.sleep(new Random().nextInt(50000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}}然后重新写一个测试类,测试代码如下:public class TimeTest { /**
* @param args
*/
public static void main(String[] args) {
for(int i= 0; i< 10; i++){
TimeThread thread= new TimeThread(i);
thread.start();
// try {
// Thread.sleep(new Random().nextInt(2));
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
} }}
Thread 2: 2008-12-04 09:34:09=====2008-12-04 09:34:15=====2008-12-04 09:34:151228354455015-----1228354455015
Thread 0: 2008-12-04 09:34:09=====2008-12-04 09:34:15=====2008-12-04 09:34:151228354455015-----1228354455015
Thread 9: 2008-12-04 09:34:09=====2008-12-04 09:34:15=====2008-12-04 09:34:151228354455015-----1228354455015
Thread 6: 2008-12-04 09:34:09=====2008-12-04 09:34:15=====2008-12-04 09:34:151228354455015-----1228354455015
Thread 8: 2008-12-04 09:34:09=====2008-12-04 09:34:15=====2008-12-04 09:34:151228354455015-----1228354455015
Thread 1: 2008-12-04 09:34:09=====2008-12-04 09:34:15=====2008-12-04 09:34:151228354455031-----1228354455031
Thread 4: 2008-12-04 09:34:09=====2008-12-04 09:34:15=====2008-12-04 09:34:151228354455031-----1228354455031
Thread 7: 2008-12-04 09:34:09=====2008-12-04 09:34:15=====2008-12-04 09:34:151228354455031-----1228354455031
Thread 5: 2008-12-04 09:34:09=====2008-12-04 09:34:15=====2008-12-04 09:34:151228354455031-----1228354455031
Thread 3: 2008-12-04 09:34:09=====2008-12-04 09:34:15=====2008-12-04 09:34:151228354455796-----1228354455796
Thread 8: 2008-12-04 09:34:15=====2008-12-04 09:34:15=====2008-12-04 09:34:15可以看到,TimeUtil.getDate()方法获得的是就比其他要早,后两个时间是一样的~
如果将上述测试类中注释的代码取消注释,则输出时间如下:1228354551203-----1228354551203
1228354551203-----1228354551203
Thread 3: 2008-12-04 09:35:51=====2008-12-04 09:35:51=====2008-12-04 09:35:51Thread 1: 2008-12-04 09:35:51=====2008-12-04 09:35:51=====2008-12-04 09:35:511228354551218-----1228354551218
Thread 2: 2008-12-04 09:35:51=====2008-12-04 09:35:51=====2008-12-04 09:35:511228354551234-----1228354551234
Thread 0: 2008-12-04 09:35:51=====2008-12-04 09:35:51=====2008-12-04 09:35:511228354551234-----1228354551234
Thread 5: 2008-12-04 09:35:51=====2008-12-04 09:35:51=====2008-12-04 09:35:511228354551234-----1228354551234
Thread 6: 2008-12-04 09:35:51=====2008-12-04 09:35:51=====2008-12-04 09:35:511228354551234-----1228354551234
Thread 4: 2008-12-04 09:35:51=====2008-12-04 09:35:51=====2008-12-04 09:35:511228354551234-----1228354551234
Thread 7: 2008-12-04 09:35:51=====2008-12-04 09:35:51=====2008-12-04 09:35:511228354551234-----1228354551234
Thread 8: 2008-12-04 09:35:51=====2008-12-04 09:35:51=====2008-12-04 09:35:511228354551234-----1228354551234
Thread 9: 2008-12-04 09:35:51=====2008-12-04 09:35:51=====2008-12-04 09:35:511228354553562-----1228354553562
Thread 3: 2008-12-04 09:35:53=====2008-12-04 09:35:53=====2008-12-04 09:35:53此情况下三种方法获得的时间是相同的
即getTime()在getDate()方法之前调用,然后打印输出方式不边,输出如下:1228354734921-----1228354734921
Thread 0: 2008-12-04 09:38:54=====2008-12-04 09:38:49=====2008-12-04 09:38:541228354734921-----1228354734921
Thread 8: 2008-12-04 09:38:54=====2008-12-04 09:38:49=====2008-12-04 09:38:541228354734921-----1228354734921
Thread 7: 2008-12-04 09:38:54=====2008-12-04 09:38:49=====2008-12-04 09:38:541228354734921-----1228354734921
Thread 6: 2008-12-04 09:38:54=====2008-12-04 09:38:49=====2008-12-04 09:38:541228354734921-----1228354734921
Thread 1: 2008-12-04 09:38:54=====2008-12-04 09:38:49=====2008-12-04 09:38:54
1228354734921-----1228354734921
1228354734921-----1228354734921
1228354734921-----1228354734921
1228354734921-----1228354734921
Thread 5: 2008-12-04 09:38:54=====2008-12-04 09:38:49=====2008-12-04 09:38:54Thread 2: 2008-12-04 09:38:54=====2008-12-04 09:38:49=====2008-12-04 09:38:54Thread 9: 2008-12-04 09:38:54=====2008-12-04 09:38:49=====2008-12-04 09:38:54Thread 4: 2008-12-04 09:38:54=====2008-12-04 09:38:49=====2008-12-04 09:38:54
1228354734921-----1228354734921
Thread 3: 2008-12-04 09:38:54=====2008-12-04 09:38:49=====2008-12-04 09:38:54从这里可以看出,getTime()获得的系统时间是要早的~如果按原帖测试代码执行,只是将getTime()在getDate()之前调用,也会出现上述情况,某次输出如下:1228355037953-----1228355037953
Thread 1: 2008-12-04 09:43:57=====2008-12-04 09:43:50=====2008-12-04 09:43:571228355037953-----1228355037953
Thread 5: 2008-12-04 09:43:57=====2008-12-04 09:43:50=====2008-12-04 09:43:571228355037953-----1228355037953
Thread 9: 2008-12-04 09:43:57=====2008-12-04 09:43:50=====2008-12-04 09:43:571228355037953-----1228355037953
Thread 0: 2008-12-04 09:43:57=====2008-12-04 09:43:50=====2008-12-04 09:43:571228355037953-----1228355037953
Thread 2: 2008-12-04 09:43:57=====2008-12-04 09:43:50=====2008-12-04 09:43:571228355037953-----1228355037953
Thread 7: 2008-12-04 09:43:57=====2008-12-04 09:43:50=====2008-12-04 09:43:571228355037953-----1228355037953
1228355037953-----1228355037953
Thread 6: 2008-12-04 09:43:57=====2008-12-04 09:43:50=====2008-12-04 09:43:571228355037953-----1228355037953
Thread 3: 2008-12-04 09:43:57=====2008-12-04 09:43:50=====2008-12-04 09:43:571228355037953-----1228355037953
Thread 8: 2008-12-04 09:43:57=====2008-12-04 09:43:50=====2008-12-04 09:43:57Thread 4: 2008-12-04 09:43:57=====2008-12-04 09:43:50=====2008-12-04 09:43:571228355038250-----1228355038250
Thread 6: 2008-12-04 09:43:58=====2008-12-04 09:43:58=====2008-12-04 09:43:58
运行一次过后,打印输出时间保持一致..
要问的就是: 1. 为何会出现上述时间不一致现象?具体问题是什么2. 关于getDate()或者getTime()方法,这样能够测试线程同步不?
线程在调用方法的时候,没有谁先谁后的说法。
java和c在这里有点不一样。
当2个线程调用getTime得时候,
第一个线程先调用
第二个线程后调用
但是谁也不能保证,第一个线程肯定是先调用return time;的。有可能第二个线程会先调用return time;
所以,在java里,双重检查机制是不起作用的。具体请参考,java与模式
public static String getTime(){
Calendar calendar= Calendar.getInstance();
Date date= calendar.getTime();
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time= dateFormat.format(date);
return time;
}
线程在调用方法的时候,没有谁先谁后的说法。
java和c在这里有点不一样。
当2个线程调用getTime得时候,
第一个线程先调用
第二个线程后调用
但是谁也不能保证,第一个线程肯定是先调用return time;的。有可能第二个线程会先调用return time;
所以,在java里,双重检查机制是不起作用的。具体请参考,java与模式