zoukankan      html  css  js  c++  java
  • Spring StopWatch源码

    import java.text.NumberFormat;
    import java.util.LinkedList;
    import java.util.List;
    
    public class StopWatch {
        private final String id;
        private boolean keepTaskList = true;
        private final List<TaskInfo> taskList = new LinkedList();
        private long startTimeMillis;
        private boolean running;
        private String currentTaskName;
        private StopWatch.TaskInfo lastTaskInfo;
        private int taskCount;
        private long totalTimeMillis;
    
        public StopWatch() {
            this.id = "";
        }
    
        public StopWatch(String id) {
            this.id = id;
        }
    
        public void setKeepTaskList(boolean keepTaskList) {
            this.keepTaskList = keepTaskList;
        }
    
        public void start() throws IllegalStateException {
            this.start("");
        }
    
        public void start(String taskName) throws IllegalStateException {
            if (this.running) {
                throw new IllegalStateException("Can't start StopWatch: it's already running");
            } else {
                this.startTimeMillis = System.currentTimeMillis();
                this.running = true;
                this.currentTaskName = taskName;
            }
        }
    
        public void stop() throws IllegalStateException {
            if (!this.running) {
                throw new IllegalStateException("Can't stop StopWatch: it's not running");
            } else {
                long lastTime = System.currentTimeMillis() - this.startTimeMillis;
                this.totalTimeMillis += lastTime;
                this.lastTaskInfo = new StopWatch.TaskInfo(this.currentTaskName, lastTime);
                if (this.keepTaskList) {
                    this.taskList.add(this.lastTaskInfo);
                }
    
                ++this.taskCount;
                this.running = false;
                this.currentTaskName = null;
            }
        }
    
        public boolean isRunning() {
            return this.running;
        }
    
        public long getLastTaskTimeMillis() throws IllegalStateException {
            if (this.lastTaskInfo == null) {
                throw new IllegalStateException("No tasks run: can't get last task interval");
            } else {
                return this.lastTaskInfo.getTimeMillis();
            }
        }
    
        public String getLastTaskName() throws IllegalStateException {
            if (this.lastTaskInfo == null) {
                throw new IllegalStateException("No tasks run: can't get last task name");
            } else {
                return this.lastTaskInfo.getTaskName();
            }
        }
    
        public StopWatch.TaskInfo getLastTaskInfo() throws IllegalStateException {
            if (this.lastTaskInfo == null) {
                throw new IllegalStateException("No tasks run: can't get last task info");
            } else {
                return this.lastTaskInfo;
            }
        }
    
        public long getTotalTimeMillis() {
            return this.totalTimeMillis;
        }
    
        public double getTotalTimeSeconds() {
            return (double) this.totalTimeMillis / 1000.0D;
        }
    
        public int getTaskCount() {
            return this.taskCount;
        }
    
        public StopWatch.TaskInfo[] getTaskInfo() {
            if (!this.keepTaskList) {
                throw new UnsupportedOperationException("Task info is not being kept!");
            } else {
                return (StopWatch.TaskInfo[]) this.taskList.toArray(new StopWatch.TaskInfo[this.taskList.size()]);
            }
        }
    
        public String shortSummary() {
            return "StopWatch '" + this.id + "': running time (millis) = " + this.getTotalTimeMillis();
        }
    
        public String prettyPrint() {
            StringBuilder sb = new StringBuilder(this.shortSummary());
            sb.append('
    ');
            if (!this.keepTaskList) {
                sb.append("No task info kept");
            } else {
                sb.append("-----------------------------------------
    ");
                sb.append("ms     %     Task name
    ");
                sb.append("-----------------------------------------
    ");
                NumberFormat nf = NumberFormat.getNumberInstance();
                nf.setMinimumIntegerDigits(5);
                nf.setGroupingUsed(false);
                NumberFormat pf = NumberFormat.getPercentInstance();
                pf.setMinimumIntegerDigits(3);
                pf.setGroupingUsed(false);
                StopWatch.TaskInfo[] var7;
                int var6 = (var7 = this.getTaskInfo()).length;
    
                for (int var5 = 0; var5 < var6; ++var5) {
                    StopWatch.TaskInfo task = var7[var5];
                    sb.append(nf.format(task.getTimeMillis())).append("  ");
                    sb.append(pf.format(task.getTimeSeconds() / this.getTotalTimeSeconds())).append("  ");
                    sb.append(task.getTaskName()).append("
    ");
                }
            }
    
            return sb.toString();
        }
    
        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder(this.shortSummary());
            if (this.keepTaskList) {
                StopWatch.TaskInfo[] var5;
                int var4 = (var5 = this.getTaskInfo()).length;
    
                for (int var3 = 0; var3 < var4; ++var3) {
                    StopWatch.TaskInfo task = var5[var3];
                    sb.append("; [").append(task.getTaskName()).append("] took ").append(task.getTimeMillis());
                    long percent = Math.round(100.0D * task.getTimeSeconds() / this.getTotalTimeSeconds());
                    sb.append(" = ").append(percent).append("%");
                }
            } else {
                sb.append("; no task info kept");
            }
    
            return sb.toString();
        }
    
        public static final class TaskInfo {
            private final String taskName;
            private final long timeMillis;
    
            TaskInfo(String taskName, long timeMillis) {
                this.taskName = taskName;
                this.timeMillis = timeMillis;
            }
    
            public String getTaskName() {
                return this.taskName;
            }
    
            public long getTimeMillis() {
                return this.timeMillis;
            }
    
            public double getTimeSeconds() {
                return (double) this.timeMillis / 1000.0D;
            }
        }
    
    }

    来源:https://blog.csdn.net/gxs1688/article/details/87185030

  • 相关阅读:
    5 Things Every Manager Should Know about Microsoft SharePoint 关于微软SharePoint每个经理应该知道的五件事
    Microsoft SharePoint 2010, is it a true Document Management System? 微软SharePoint 2010,它是真正的文档管理系统吗?
    You think you use SharePoint but you really don't 你认为你使用了SharePoint,但是实际上不是
    Introducing Document Management in SharePoint 2010 介绍SharePoint 2010中的文档管理
    Creating Your Own Document Management System With SharePoint 使用SharePoint创建你自己的文档管理系统
    MVP模式介绍
    权重初始化的选择
    机器学习中线性模型和非线性的区别
    神经网络激励函数的作用是什么
    深度学习中,交叉熵损失函数为什么优于均方差损失函数
  • 原文地址:https://www.cnblogs.com/heyang78/p/11834204.html
Copyright © 2011-2022 走看看