zoukankan      html  css  js  c++  java
  • 定时器

    Timer的核心代码:
    private void mainLoop() {   
            while (true) {   
                try {   
                    TimerTask task;   
                    boolean taskFired = false;   
                    synchronized (queue) {   
                        while (queue.isEmpty() && newTasksMayBeScheduled) {   
                            queue.wait();   
                        }   
                        if (queue.isEmpty())   
                            break; // 直接挑出mainLoop了.   
                        long currentTime, executionTime;   
                        task = queue.getMin(); // 获取这个任务队列第一个任务   
                        synchronized (task.lock) {   
                            if (task.state == TimerTask.CANCELLED) {   
                                queue.removeMin();   
                                continue;   
                            }   
                            currentTime = System.currentTimeMillis();   
                            executionTime = task.nextExecutionTime;   
                            if (taskFired = (executionTime <= currentTime)) {   
                                if (task.period == 0) { // Non-repeating, remove   
                                    queue.removeMin();   
                                    task.state = TimerTask.EXECUTED;   
                                } else { // Repeating task, reschedule   
                                    queue.rescheduleMin(task.period < 0 ? currentTime - task.period : executionTime   
                                            + task.period);   
                                }   
                            }   
                        }//释放锁   
                        if (!taskFired)   
                            queue.wait(executionTime - currentTime);   
                    }   
                    if (taskFired) // Task fired; run it, holding no locks   
                        task.run();   
                } catch (InterruptedException e) {   
                }   
            }// while(true)   
        } 
     
    但是Timer和TimerTask存在一些缺陷:

    1:Timer只创建了一个线程。当你的任务执行的时间超过设置的延时时间将会产生一些问题。

     
    2:Timer创建的线程没有处理异常,因此一旦抛出非受检异常,该线程会立即终止。
     
    JDK 5.0以后推荐使用ScheduledThreadPoolExecutor。该类属于Executor Framework,它除了能处理异常外,还可以创建多个线程解决上面的问题。
    使用:任务继承 Runnable 接口来执行线程
  • 相关阅读:
    一些 Ubuntu 使用的小技巧
    体验 Web 自动化测试工具 Selenium
    CentOS 7 上安装 Nginx
    Windows查看端口占用情况
    Windows远程登录提醒:由于没有远程桌面授权服务器可以提供许可证,远程会话连接已断开。请跟服务器管理员联系。
    Vue动态的改变css样式
    centos7 U盘安装卡在 starting dracut initqueue hook Reached target Basic System
    用tsc编译ts文件的时候报错,tsc : 无法加载文件,因为在此系统上禁止运行脚本;
    Linux修改SSH默认的端口号
    Centos编译安装新版本Git
  • 原文地址:https://www.cnblogs.com/wanglao/p/5329678.html
Copyright © 2011-2022 走看看