线程本地变量ThreadLocal (耗时工具)【原】

 线程本地变量类

package king;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
 
/**
 * TLTimeContainer为ThreadLocalTimeContainer(线程本地变量时间容器)的缩写
 * 说明:用来在任意一个方法内部置入recordTime(),以作片断时间点的记录
 * @author King
 * @time 2015/10/29
 */
public class TLTimeContainer {
    public static ThreadLocal<Map<String,List<Long>>> tl = new ThreadLocal<Map<String,List<Long>>>();
 
    /**
     * 记录调用本方法的 类的方法 起,止时间
     */
    public static void recordTime() {
        String clazz = Thread.currentThread().getStackTrace()[2].getClassName();
        String method = Thread.currentThread().getStackTrace()[2].getMethodName();
        Long l = System.currentTimeMillis();
        if (tl.get() == null) {
            Map<String,List<Long>> outerMap = new TreeMap<String,List<Long>>();
            List<Long> list1 = new ArrayList<Long>();
            list1.add(System.currentTimeMillis());
            outerMap.put("类"+clazz+"->"+"方法"+method+" ", list1);
            tl.set(outerMap);
        } else {
            Map<String,List<Long>> outerMap= tl.get();
            if(outerMap != null){
                List<Long> list2 = null;
                list2 = outerMap.get("类"+clazz+"->"+"方法"+method+" ");
                if(list2 != null){
                    list2.add(System.currentTimeMillis());
                }else{
                    list2 = new ArrayList<Long>();
                    list2.add(System.currentTimeMillis());
                    outerMap.put("类"+clazz+"->"+"方法"+method+" ", list2);
                }
            }
        }
    }
 
    /**
     * 打印放入本容器中的 类的方法的起,止时间和耗时
     */
    public static void print(){
        Map<String,List<Long>> outerMap= tl.get();
        if (outerMap != null) {
            for (Entry<String, List<Long>> entry : outerMap.entrySet()) {
//                 System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
                   List<Long> list = entry.getValue();
                   Long start = list.get(0);
                   if(list.size() == 2){
                       Long end = list.get(1);
                       System.out.println(entry.getKey()+"起于:"+start);
                       System.out.println(entry.getKey()+"止于:"+end);
                       System.out.println(entry.getKey()+"耗时:"+(end-start));
                   }else{
                       System.out.println(entry.getKey()+"起于:"+start);
                   }
                   System.out.println("__________________________________________");
            }
        }
        clearAll();
    }
     
     
    /**
     * 清除所有
     */
    public static void clearAll(){
        tl.remove();
    }
     
    /**
     * 清除单个对象
     */
    public static void remove(Map map) {
        if (tl.get() != null) {
            tl.get().remove(map);
        }
    }
}

  

  入口类

package king;

public class Entrance {
    public static void main(String[] args) throws Exception{
        method1();
        method2();
        TLTimeContainer.print();
        TLTimeContainer.clearAll();
    }

    public static void method1() throws Exception {
        TLTimeContainer.recordTime();// 方法入口调用
        Thread.sleep(1000);
        TLTimeContainer.recordTime();// 方法出口调用
    }

    public static void method2() throws Exception {
        TLTimeContainer.recordTime();// 方法入口调用
        Thread.sleep(2000);
        TLTimeContainer.recordTime();// 方法出口调用
    }
}

 

  

  运行后打印结果:

类compare.Other->方法m1 起于:1446088276528
类compare.Other->方法m1 止于:1446088277528
类compare.Other->方法m1 耗时:1000
__________________________________________
类compare.Other->方法m2 起于:1446088277528
类compare.Other->方法m2 止于:1446088279529
类compare.Other->方法m2 耗时:2001
__________________________________________

  源码下载: http://pan.baidu.com/s/1jGlOdvw

转载于:https://www.cnblogs.com/whatlonelytear/p/4919883.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值