A simple example for Thread Local concept in Java:
public
ThreadLocalExamples tl =
new ThreadLocalExamples();
T1 t1 = tl.
new T1();
t1.start();
T2 t2 = tl.
new T2();
t2.start();
System.
out.println("main thread->"+tlocal.get());
t1.join();
t2.join();
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.
out.println(x);
}
System.
out.println("value in t1->"+tlocal.get());
System.
out.println("value in t1->"+tlocal.get());
}
}
System.
out.println("value in t2->"+ tlocal.get());
}
}
}
Output:
main thread->100
value in t1->null
value in t1->200
value in t2->null
2
No comments:
Post a Comment