时间类,获取系统的毫秒级时间
public class StupidTime {
private int YY;
private int MM;
private int DD;
private int HH;
private int mm;
private int SS;
private int MI;
public StupidTime(Calendar Cld ) {
Cld = Calendar.getInstance();
YY = Cld.get(Calendar.YEAR) ;
MM = Cld.get(Calendar.MONTH)+1;
DD = Cld.get(Calendar.DATE);
HH = Cld.get(Calendar.HOUR_OF_DAY);
mm = Cld.get(Calendar.MINUTE);
SS = Cld.get(Calendar.SECOND);
MI = Cld.get(Calendar.MILLISECOND);
}
@Override
public String toString() {
return "StupidTime [ " + YY + "/" + MM + "/" + DD + " " + HH + ":" + mm + ":" + SS
+ ":" + MI + " ]";
}
}
线程类
public class EatThread implements Runnable {
private Thread t;
private String oneSillyName;
private int sleep;
public EatThread(String osn, int sleep) {
oneSillyName = osn;
this.sleep = sleep;
}
@Override
public void run() {
// TODO 自动生成的方法存根
// 输出四次方便观察每个线程输出的时间顺序
try {
for(int i = 0; i < 4; i++) {
Calendar c = Calendar.getInstance();
StupidTime st = new StupidTime(c);//创建当前时间
System.out.println("第"+(i+1)+"次: "+oneSillyName + st.toString() );
Thread.sleep(sleep);
}
}catch (Exception e) {
System.out.println(e);
}
}
public void start () {
if(t == null) {
t = new Thread(this, oneSillyName);
t.start();
}
}
}
主类Test
/**
* @category Thread
* @author Clay
* @version .8.1
*/
public class Test {
public static void main(String args[]) {
EatThread eatThread1 = new EatThread("吃饭一秒", 1000);
EatThread eatThread2 = new EatThread("吃菜两秒", 2000);
EatThread eatThread3 = new EatThread("洗碗三秒", 3000);
eatThread1.start();
eatThread2.start();
eatThread3.start();
}
}
运行结果