+/EE360P
distributed systems
git clone https://git.julia.blue/+/EE360P
cc0c4d5
master
1// http://tutorials.jenkov.com/java-concurrency/threadlocal.html 2public class ThreadLocalExample2 { 3 4 5public static class MyRunnable implements Runnable { 6 7private ThreadLocal <Integer >threadLocal = 8new ThreadLocal <Integer >(); 9private int plain ; 10 11@ Override 12public void run () { 13threadLocal .set ( (int ) (Math .random () *100D ) ); 14System .out .println (Thread .currentThread ().getName () +" has ThreadLocal " +threadLocal .get ()); 15 16plain = (int ) (Math .random () *100D ) ; 17System .out .println (Thread .currentThread ().getName () +" has plain " +plain ); 18 19try { 20Thread .sleep (2000 ); 21 }catch (InterruptedException e ) { 22 } 23 24System .out .println (Thread .currentThread ().getName () +" has ThreadLocal " +threadLocal .get ()); 25System .out .println (Thread .currentThread ().getName () +" has plain " +plain ); 26 } 27 } 28 29 30public static void main (String []args )throws InterruptedException { 31MyRunnable sharedRunnableInstance =new MyRunnable (); 32 33Thread thread1 =new Thread (sharedRunnableInstance ,"Thread 1" ); 34Thread thread2 =new Thread (sharedRunnableInstance ,"Thread 2" ); 35 36thread1 .start (); 37thread2 .start (); 38 39thread1 .join ();//wait for thread 1 to terminate 40thread2 .join ();//wait for thread 2 to terminate 41 } 42 43} 44