import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
public class StackOverFlowDemo02 {
public static void main(String[] args) throws InterruptedException {
final int[] arr = {1};
while (arr[0] > 0) {
CountDownLatch latch = new CountDownLatch(1);
Thread t = new Thread() {
public void run() {
System.out.println("Round: " + arr[0]);
try {
printTimestamp();
Thread.sleep(5000);
printTimestamp();
} catch (InterruptedException e) {
e.printStackTrace();
}
latch.countDown();
}
};
t.start();
latch.await();
arr[0] += 1;
System.out.println("--------------------------------------------");
}
System.out.println("Loop finished.");
}
private static void printTimestamp() {
Long ts = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
//SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
Date date = new Date(ts);
System.out.println("Date: " + sdf.format(date));
}
}
Round: 1
Date: 2018-03-06 15:33:32:085
Date: 2018-03-06 15:33:37:160
--------------------------------------------
Round: 2
Date: 2018-03-06 15:33:37:160
Date: 2018-03-06 15:33:42:162
--------------------------------------------
Round: 3
Date: 2018-03-06 15:33:42:163
Date: 2018-03-06 15:33:47:163
--------------------------------------------
Round: 4
Date: 2018-03-06 15:33:47:164
Date: 2018-03-06 15:33:52:165
--------------------------------------------
Round: 5
Date: 2018-03-06 15:33:52:165
Date: 2018-03-06 15:33:57:166
--------------------------------------------
Round: 6
Date: 2018-03-06 15:33:57:167
Date: 2018-03-06 15:34:02:168
--------------------------------------------
....................................