java 实现TimerController

来源:互联网 发布:电脑语音录入软件 编辑:程序博客网 时间:2024/06/02 04:50

今天笔试遇见一道很有意思的题。题的大概意思是实现一个Timer来进行任务调度,即加入一个任务,然后任务就会在一定时间之后被执行。其实这道题本身并不难,但在实际操作的时候我碰见了很多问题,比如说存储timer是用Map还是LinkedList。我的第一反应是用Map, 速度快啊,但写到遍历的时候旧懵了,我一直以来都以为map的遍历是后list一样,哎,不等不删掉代码选用了速度更慢的后者。另一大问题是在构思的时候我一开始选择用两个容器(即两个map或两个list)来分别存放任务和时间,但写到最后才发现自己好傻b,应该用一个类把这两个变量封装起来啊!!!于是终于写出了如下的代码:

import java.util.*;/* * TimerController 是一个计时器控制器,当加入的timer达到运行时间时该timer的Thread就会执行 */public  class TimerController {static int TimerID = 0;static LinkedList<timer> timerList = new LinkedList();static Map<Integer,timer> map = new HashMap();static boolean shouldRun = false;Thread MainThread = new Thread(){public void run(){while(shouldRun){Inspected();try {Thread.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}}}};/* *开启TimerController,同时TimerController的主线程开始工作 */public void start(){if(!shouldRun){shouldRun = true;MainThread.start();}}/* * 停止TimerController。 */public void stop(){shouldRun = false;}/* * @time 需要等待的时间 * @t    需要执行的线程 * @return int 返回加入的timer的id  */public int startTimer(int time,Thread t){map.put(TimerID,new timer(TimerID,time,t));return TimerID++;}/* * @id 需要停止的timer的id */public void stopTimer(int id){if(map.containsKey(id)){map.remove(id);}}private void Inspected(){Stack<Integer> stack = new Stack<>();if(!map.isEmpty()){for(int id : map.keySet()){if(!map.get(id).Inspected()){stack.push(id);}}}while(!stack.empty()){map.remove(stack.pop());}}}class timer{int id;int waitTime;Thread mission;boolean hasRun = false;timer(int id,int waitTime,Thread mission){this.id = id;this.waitTime = waitTime;this.mission = mission;}public boolean Inspected(){if(hasRun){return false;}{waitTime = waitTime - 10;if(waitTime <0){mission.start();return false;}return true;}}} 

这是demo:

public class test {public static void main(String[] args) {//创建三个线程Thread t1 = new Thread(){public void run(){System.out.println("Thread1 is running!!!");}};Thread t2 = new Thread(){public void run(){System.out.println("Thread2 is running!!!");}};Thread t3 = new Thread(){public void run(){System.out.println("Thread3 is running!!!");}};//获得一个TimerController对象TimerController tc = new TimerController();//为TimerController添加timerSystem.out.println("Thread1所在timer的id = "+tc.startTimer(1000, t1));System.out.println("Thread2所在timer的id = "+tc.startTimer(2000, t2));System.out.println("Thread3所在timer的id = "+tc.startTimer(3000, t3));//启动TimerControllerSystem.out.println("TimerController start");tc.start();//主线程等待2sSystem.out.println("MainThread sleep");try {Thread.sleep(2000);} catch (InterruptedException e) {// TODO 自动生成的 catch 块e.printStackTrace();}//停止id==2的timer,即Thread3所在的timerSystem.out.println("MainThread wake");System.out.println("停止id = 2 的timer");tc.stopTimer(2);System.out.println("MainThread sleep");//主线程等待2stry {Thread.sleep(2000);} catch (InterruptedException e) {// TODO 自动生成的 catch 块e.printStackTrace();}System.out.println("MainThread wake");System.out.println("TimerController stop");//停止TimerControllertc.stop();}}
运行结果:

Thread1所在timer的id = 0Thread2所在timer的id = 1Thread3所在timer的id = 2TimerController startMainThread sleepThread1 is running!!!MainThread wake停止id = 2 的timerMainThread sleepThread2 is running!!!MainThread wakeTimerController stop




0 0