package com.chenyou.admin.util;
/**
* @see
* @author Al_assad yulinying_1994@outlook.com
* @date 2020年10月18日 上午2:47:44
* @version V1.0
* Description: 倒计时实现方式2:使用java.uitl.Timer实现
* 使用两个线程
*/
import com.chenyou.admin.dao.TaskInfoStageTimeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
@Component
public class CountDownUtil {
@Autowired
private TaskInfoStageTimeDao taskInfoStageTimeDao;
public void CountDownUtil(Integer id,int limitSec){
try {
final int[] curSec = {limitSec};
System.out.println("count down from "+limitSec+" s ");
Timer timer = new Timer();
timer.schedule(new TimerTask(){
public void run(){
System.out.println("Time remians "+ --curSec[0] +" s");
}
},0,1000);
TimeUnit.SECONDS.sleep(limitSec);
timer.cancel();
System.out.println("Time is out!");
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date=df.format(new Date());
taskInfoStageTimeDao.updateOneEndStage(id,0,date);
}catch (Exception e){
e.printStackTrace();
}
}
}