2014年7月1日 星期二

模擬多執行續同時執行進行測試

/**
 * @author powei.chen
 * 模擬多執行續同時執行進行測試
 */
public class MultiThread {

long startTime;

public static void main(String[] args) {
new MultiThread();
}

public MultiThread() {
startTime = new Date().getTime() + 5000;
for (int i = 0; i < 5; i++) {
Test test = new Test(i);
test.start();
}
}

class Test extends Thread {
private int no;

public Test(int no) {
this.no = no;
try {
Thread.sleep(500);// 此處表示new是需要時間的
} catch (Exception e) {
e.printStackTrace();
}
}

public void run() {
try {
Thread.sleep(startTime - new Date().getTime());// 以此方式達到同步
//TODO 執行測試
System.out.println(no + " : " + new Date());
} catch (Exception e) {
e.printStackTrace();
}
}
}

}

沒有留言:

張貼留言