java多线程实现假期倒计时
时间: 2023-12-27 12:25:48 浏览: 91
以下是一个Java多线程实现假期倒计时的例子:
```java
import java.text.SimpleDateFormat;
import java.util.Date;
public class CountdownThread extends Thread {
private int days;
public CountdownThread(int days) {
this.days = days;
}
@Override
public void run() {
Date startTime = new Date(System.currentTimeMillis());
while (days > 0) {
try {
Thread.sleep(1000);
System.out.println("距离假期还有 " + days + " 天");
startTime = new Date(System.currentTimeMillis());
days--;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("假期开始!");
}
public static void main(String[] args) {
CountdownThread countdownThread = new CountdownThread(7);
countdownThread.start();
}
}
```
阅读全文