Player.java
import java.util.concurrent.CountDownLatch; public class Player implements Runnable { private int id; private CountDownLatch begin; private CountDownLatch end; public Player(int i, CountDownLatch begin, CountDownLatch end) { super(); this.id = i; this.begin = begin; this.end = end; } @Override public void run() { try{ begin.await(); //等待begin的状态为0 Thread.sleep((long)(Math.random()*3000)); //随机分配时间,即运动员完成时间 System.out.println("Play"+id+" arrived."); }catch (InterruptedException e) { System.out.println(e); }finally{ end.countDown(); //使end状态减1,最终减至0 } } }
MyTask.java
import java.util.TimerTask; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class MyTask extends TimerTask { public static final int PLAYER_AMOUNT = 5; public void run() { System.out.println("------------"); // System.out.println("call at " + (new Date())); // TODO 此处添加具体任务代码 // 对于每位运动员,CountDownLatch减1后即结束比赛 ExecutorService exe = Executors.newFixedThreadPool(PLAYER_AMOUNT); try { CountDownLatch begin = new CountDownLatch(1); // 对于整个比赛,所有运动员结束后才算结束 CountDownLatch end = new CountDownLatch(PLAYER_AMOUNT); Player[] plays = new Player[PLAYER_AMOUNT]; for (int i = 0; i < PLAYER_AMOUNT; i++) plays[i] = new Player(i , begin, end); // 设置特定的线程池,大小为5 for (Player p : plays) exe.execute(p); // 分配线程 System.out.println("Race begins!"); begin.countDown(); end.await(); // 等待end状态变为0,即为比赛结束 } catch (InterruptedException e) { System.out.println(e); } finally { System.out.println("Race ends!"); exe.shutdown(); } } }
ThreadDemo.java
import java.util.Timer; public class ThreadDemo { public void demo(){ Timer timer = new Timer(false);// true 指定为后台线程 // 设置任务计划,启动和间隔时间 timer.scheduleAtFixedRate(new MyTask(), 0, 2000); // 可写多个定时任务 /*还有好几种任务调度 timer.schedule(task, time); // time为Date类型:在指定时间执行一次。 timer.schedule(task, firstTime, period); // firstTime为Date类型,period为long // 从firstTime时刻开始,每隔period毫秒执行一次。 timer.schedule(task, delay) // delay 为long类型:从现在起过delay毫秒执行一次 timer.schedule(task, delay, period) // delay为long,period为long:从现在起过delay毫秒以后,每隔period // 毫秒执行一次。 */ /*/时间间隔 private static final long PERIOD_DAY = 24 * 60 * 60 * 1000; public TimerManager() { Calendar calendar = Calendar.getInstance(); *//*** 定制每日2:00执行方法 ***//* calendar.set(Calendar.HOUR_OF_DAY, 2); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); Date date=calendar.getTime(); //第一次执行定时任务的时间 //如果第一次执行定时任务的时间 小于 当前的时间 //此时要在 第一次执行定时任务的时间 加一天,以便此任务在下个时间点执行。如果不加一天,任务会立即执行。 if (date.before(new Date())) { date = this.addDay(date, 1); } Timer timer = new Timer(); NFDFlightDataTimerTask task = new NFDFlightDataTimerTask(); //安排指定的任务在指定的时间开始进行重复的固定延迟执行。 timer.schedule(task,date,PERIOD_DAY); } // 增加或减少天数 public Date addDay(Date date, int num) { Calendar startDT = Calendar.getInstance(); startDT.setTime(date); startDT.add(Calendar.DAY_OF_MONTH, num); return startDT.getTime(); }*/ } /** * @param args */ public static void main(String[] args) { new ThreadDemo().demo(); } }