package com.shk.cn.shk_hoonk;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
//未完成待续 哈哈哈
public class DynanicDemo {
public static void main(String[] args) throws Exception {
new DynanicDemo().callBackScheduleExection();
}
public void callBackScheduleExection() throws Exception {
ScheduledExecutorService newScheduledThreadPool = Executors.newScheduledThreadPool(4);
Runnable runnable2 = new Runnable() {
public void run() {
System.out.println("helloWorld!");
}
};
Runnable runnable = new Runnable() {
@Override
public void run() {
Date parse =null;
try {
String currentDate= new SimpleDateFormat("yy-MM-dd HH:mm:ss").format(new Date(System.currentTimeMillis()));
String substring = currentDate.substring(0, currentDate.lastIndexOf(" "));
parse = new SimpleDateFormat("yy-MM-dd HH:mm:ss").parse(substring+" 18:00:00");
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
long seconds= (parse.getTime()-System.currentTimeMillis())/1000;
System.out.println("离下班还有" + seconds + "秒" +"\t"+ seconds/60 +"分钟"+ "\t" +seconds/3600+"小时"+"\t|"+"当前时间:" + new Date().toLocaleString());
}
};
final ScheduledFuture<?> scheduleAtFixedRate = newScheduledThreadPool.scheduleAtFixedRate(runnable, 2, 1, TimeUnit.SECONDS);
Runnable runnable3 = new Runnable() {
public void run() {
// long delay = scheduleAtFixedRate.getDelay(TimeUnit.SECONDS);
// System.out.println("runnable3======delay"+ delay);
System.out.println(scheduleAtFixedRate.isCancelled()+":" +scheduleAtFixedRate.isDone());
if (!scheduleAtFixedRate.isCancelled())
{
scheduleAtFixedRate.cancel(true);
}
try {
scheduleAtFixedRate.wait(3000);//等待3秒钟继续执行Runnable
scheduleAtFixedRate.notifyAll();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
Runnable runnable4 = new Runnable() {
public void run() {
// long delay = scheduleAtFixedRate.getDelay(TimeUnit.SECONDS);
// System.out.println("runnable3======delay"+ delay);
System.out.println(scheduleAtFixedRate.isCancelled()+":" +scheduleAtFixedRate.isDone());
if (!scheduleAtFixedRate.isCancelled())
{
scheduleAtFixedRate.cancel(true);
}
try {
scheduleAtFixedRate.wait(3000);//等待3秒钟继续执行Runnable
scheduleAtFixedRate.notifyAll();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
String currentDate= new SimpleDateFormat("yy-MM-dd HH:mm:ss").format(new Date(System.currentTimeMillis()));
String substring = currentDate.substring(0, currentDate.lastIndexOf(" "));
newScheduledThreadPool.scheduleAtFixedRate(runnable3,
(new SimpleDateFormat("yy-MM-dd HH:mm:ss")
.parse(substring+" 18:00:00").
getTime()-System.currentTimeMillis())/1000
, 1, TimeUnit.SECONDS);
// newScheduledThreadPool.scheduleAtFixedRate(runnable4,
// (new SimpleDateFormat("yy-MM-dd HH:mm:ss")
// .parse(substring+" 8:00:00").
// getTime()-System.currentTimeMillis())/1000
// , 1, TimeUnit.SECONDS);
//
final ScheduledFuture<?> scheduleAtFixedRate2 = newScheduledThreadPool.scheduleAtFixedRate(runnable2, 2, 2, TimeUnit.SECONDS);
boolean done = scheduleAtFixedRate2.isDone();
boolean cancelled = scheduleAtFixedRate2.isCancelled();
System.out.println(done+":" +cancelled);
//scheduleAtFixedRate.cancel(true);
// newScheduledThreadPool.awaitTermination(5, TimeUnit.SECONDS);
//
//
//
// newScheduledThreadPool.execute(runnable2);
}
}