public class Test{
//定义ThreadLocal
private static ThreadLocal name;
public static void main(String[] args) throws Exception{
name = new ThreadLocal();
//Define Thread A
Thread a = new Thread(){
public void run(){
System.out.println("Before invoke set,value is:"+name.get());
name.set(“Thread A”);
System.out.println("After invoke set, value is:"+name.get());
}
}
;
//Define Thread B
Thread b = new Thread(){
public void run(){
System.out.println("Before invoke set,value is :"+name.get());
name.set(“Thread B”);
System.out.println("After invoke set,value is :"+name.get());
}
}
;
// Not invoke set, print the value is null
System.out.println(name.get());
// Invoke set to fill a value
name.set(“Thread Main”);
// Start thread A
a.start();
a.join();
// Print the value after changed the value by thread A
System.out.println(name.get());
// Start thread B
b.start();
b.join();
// Print the value after changed the value by thread B
System.out.println(name.get())
}
}
// Class that statistic the cost time
public class StatisticCostTime{
// record the startTime
// private ThreadLocal startTime = new ThreadLocal();
private long startTime;
// private ThreadLocal costTime = new ThreadLocal();
private long costTime;
private StatisticCostTime(){
}
//Singleton
public static final StatisticCostTime shareInstance(){
return InstanceFactory.instance;
}
private static class InstanceFactory{
private static final StatisticCostTime instance = new StatisticCostTime();
}
// start
public void start(){
// startTime.set(System. nanoTime ());
startTime = System.nanoTime();
}
// end
public void end(){
// costTime.set(System. nanoTime () - startTime.get());
costTime = System.nanoTime() - startTime;
}
public long getStartTime(){
return startTime;
// return startTime.get();
}
public long getCostTime(){
// return costTime.get();
return costTime;
}
public class Main{
public static void main(String[] args) throws Exception{
// Define the thread a
Thread a = new Thread(){
public void run(){
try{
// start record time
StatisticCostTime.shareInstance().start();
sleep(200);
// print the start time of A
System.out.println("A-startTime:"+StatisticCostTime.shareInstance().getStartTime());
// end the record
StatisticCostTime.shareInstance().end();
// print the costTime of A
System.out.println("A:"+StatisticCostTime.shareInstance().getCostTime());
}
catch(Exception e){
}
}
}
;
// start a
a.start();
// Define thread b
Thread b = new Thread(){
public void run(){
try{
// record the start time of B1
StatisticCostTime.shareInstance().start();
sleep(100);
// print the start time to console
System.out.println("B1-startTime:"+StatisticCostTime.shareInstance().getStartTime());
// end record start time of B1
StatisticCostTime.shareInstance().end();
// print the cost time of B1
System.out.println("B1:"+StatisticCostTime.shareInstance().getCostTime());
// start record time of B2
StatisticCostTime.shareInstance().start();
sleep(100);
// print start time of B2
System.out.println("B2-startTime:"+StatisticCostTime.shareInstance().getStartTime());
// end record time of B2
StatisticCostTime.shareInstance().end();
// print cost time of B2
System.out.println("B2:"+StatisticCostTime.shareInstance().getCostTime());
}
catch(Exception e){
}
}
}
;
b.start();
}
}
/**
* Sets the value of this variable for the current thread. If set to
* {@code null}, the value will be set to null and the underlying entry will
* still be present.
*
* @param value the new value of the variable for the caller thread.
*/
public void set(T value) {
Thread currentThread = Thread.currentThread();
Values values = values(currentThread);
if (values == null) {
values = initializeValues(currentThread);
}
values.put(this, value);
}
/**
* Returns the value of this variable for the current thread. If an entry
* doesn't yet exist for this variable on this thread, this method will
* create an entry, populating the value with the result of
* {@link #initialValue()}.
*
* @return the current value of the variable for the calling thread.
*/
@SuppressWarnings("unchecked")
public T get() {
// Optimized for the fast path.
Thread currentThread = Thread.currentThread();
Values values = values(currentThread);
if (values != null) {
Object[] table = values.table;
int index = hash & values.mask;
if (this.reference == table[index]) {
return (T) table[index + 1];
}
} else {
values = initializeValues(currentThread);
}
return (T) values.getAfterMiss(this);
}
/**
* Creates Values instance for this thread and variable type.
*/
Values initializeValues(Thread current) {
return current.localValues = new Values();
}
/**
* Gets Values instance for this thread and variable type.
*/
Values values(Thread current) {
return current.localValues;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有