开一个线程,显示无限循环的时间:
public class SingleTest {
public static void main(String[] args) {
ExecutorService singleExecutor = Executors.newSingleThreadExecutor();
singleExecutor.execute(new Runnable() {
@Override
public void run() {
while (true) {
String dateStr = DateFormat.getTimeInstance(DateFormat.LONG, Locale.CHINA).format(System.currentTimeMillis());
System.out.println(dateStr);
try {
Thread.sleep(1_000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
});
}
}
![](https://img-blog.csdnimg.cn/4c736b71d8534ac3a2cb3b3e8982ba0a.png)