Skip to content

Commit

Permalink
fix monitor command time stat problem(alibaba#942)
Browse files Browse the repository at this point in the history
  • Loading branch information
huangjIT authored and hengyunabc committed Nov 20, 2019
1 parent 653b217 commit 107da67
Showing 1 changed file with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
package com.taobao.arthas.core.util;

import java.util.Stack;

/**
* 简单的调用计时器。TODO 用stack的实现更合理
* 简单的调用计时器。
* Created by vlinux on 16/6/1.
* @author hengyunabc 2016-10-31
*/
public class ThreadLocalWatch {

private final ThreadLocal<Long> timestampRef = new ThreadLocal<Long>() {
private final ThreadLocal<Stack<Long>> timestampRef = new ThreadLocal<Stack<Long>>() {
@Override
protected Long initialValue() {
return System.nanoTime();
protected Stack<Long> initialValue() {
return new Stack<Long>();
}
};

public long start() {
final long timestamp = System.nanoTime();
timestampRef.set(timestamp);
timestampRef.get().push(timestamp);
return timestamp;
}

public long cost() {
return (System.nanoTime() - timestampRef.get());
return (System.nanoTime() - timestampRef.get().pop());
}

public double costInMillis() {
return (System.nanoTime() - timestampRef.get()) / 1000000.0;
return (System.nanoTime() - timestampRef.get().pop()) / 1000000.0;
}

public void clear() {
Expand Down

0 comments on commit 107da67

Please sign in to comment.