下班倒计时

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);
	}
	 

	
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值