线程本地变量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

你可能感兴趣的:(线程本地变量ThreadLocal (耗时工具))