题目: 请用程序解析ThreadLocal原理,并用程序展示其作用。 答: /** * */ package mythread. import java.util.Collections. import java.util.Map. import java.util.HashMap. import java.util.Random. /** * Those code show you muti threads use thread local to keep his own variable or object, * and make sure his variable keep the same all the time! * */ public class MyThreadLocal { /** * Main Enter * @param args */ public static void main(String[] args) { ThreadTest tt=new ThreadTest(). Thread t1=new Thread(tt,"thread 1"). Thread t2=new Thread(tt,"thread 2"). Thread t3=new Thread(tt,"thread 3"). t1.setPriority(Thread.MAX_PRIORITY). t1.start(). t2.start(). t3.start(). } } /** * This thread have use thread local implements MyThreadLocalImpl * to keep mutil thread s variable, this is a key * */ class ThreadTest implements Runnable{ private static MyThreadLocalImpl threadlocal=new MyThreadLocalImpl(). @Override public void run() { String currentThread=Thread.currentThread().getName(). System.out.println(currentThread " runing"). VO vo = getVO(). Random random = new Random(). int age = random.nextInt(100). String value=String.valueOf(age). vo.setAge(value). System.out.println(currentThread "set age: " vo.getAge()). System.out.println(currentThread " first time get age: " vo.getAge()). try { Thread.sleep(2000). } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(). } System.out.println(currentThread " second get age: " vo.getAge()). try { Thread.sleep(2000). } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(). } System.out.println(currentThread " third get age: " vo.getAge()). //clear up clearVO(). } /** * Get object from threadlocal * @return */