package org.banyan.concurrent.base;

import java.util.Date;

/**
 * User: krisjin
 * Date: 2016/2/1
 */
public class ThreadLocalExample {
    static volatile int count = 0;

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new MyRunnable(), "Thread-01");
        Thread t2 = new Thread(new MyRunnable(), "Thread-02");

        t1.start();
        t1.join();
        t2.start();
        t2.join();
    }

    static class MyRunnable implements Runnable {
        ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>();

        @Override
        public void run() {
            threadLocal.set((int) (Math.random() * 10));
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " value is " + threadLocal.get() + " , count is " + ++count + " ,date is " + new Date());

        }
    }
}

@@ -34,32 +34,8 @@
 e;%0A%0A
-import java.util.Date;%0A%0A
 /**%0A
@@ -115,176 +115,547 @@
     
-static volatile int count = 0;%0A%0A    public static void main(String%5B%5D args) throws InterruptedException %7B%0A        Thread t1 = new Thread(new MyRunnable(), %22Thread-01%22
+ThreadLocal%3CString%3E pinThreadLocal = new ThreadLocal%3C%3E();%0A%0A    public void setPin(String pin) %7B%0A        pinThreadLocal.set(pin + %22: %22 + Thread.currentThread().getId() + %22: %22 + Thread.currentThread().getName());%0A    %7D%0A%0A    public String getPin() %7B%0A        return pinThreadLocal.get();%0A    %7D%0A%0A    public static void main(String%5B%5D args) throws InterruptedException %7B%0A        ThreadLocalExample threadLocalExample = new ThreadLocalExample();%0A        threadLocalExample.setPin(%222020%22);%0A        System.out.println(threadLocalExample.getPin()
 );%0A
+%0A
     
@@ -666,17 +666,17 @@
 Thread t
-2
+1
  = new T
@@ -700,26 +700,30 @@
 ble(
-), %22Thread-02%22
+threadLocalExample)
 );%0A
-%0A
     
@@ -769,37 +769,55 @@
     
-t2.start();%0A        t2.jo
+System.out.println(threadLocalExample.getP
 in()
+)
 ;%0A  
@@ -890,25 +890,23 @@
 eadLocal
-%3CInteger%3E
+Example
  threadL
@@ -913,195 +913,141 @@
 ocal
- = new ThreadLocal%3CInteger%3E();%0A%0A        @Override%0A        public void run() %7B%0A            threadLocal.set((int) (Math.random() * 10));%0A            try %7B%0A                Thread.sleep(3000)
+Example;%0A%0A        public MyRunnable(ThreadLocalExample threadLocalExample) %7B%0A            this.threadLocalExample = threadLocalExample
 ;%0A  
@@ -1056,98 +1056,86 @@
     
+%7D%0A%0A
     
-%7D catch (InterruptedException e) %7B%0A                e.printStackTrace(
+    public void run() %7B%0A            threadLocalExample.setPin(%222021%22
 );%0A
-            %7D%0A
     
@@ -1165,134 +1165,38 @@
 tln(
-T
+t
 hread
-.currentThread().getName() + %22 value is %22 + threadLocal.get() + %22 , count is %22 + ++count + %22 ,date is %22 + new Date
+LocalExample.getPin
 ());%0A
-%0A
     

update test