// http://tutorials.jenkov.com/java-concurrency/threadlocal.html public class ThreadLocalExample { public static class MyRunnable implements Runnable { private ThreadLocal threadLocal = new ThreadLocal(); @Override public void run() { threadLocal.set( (int) (Math.random() * 100D) ); System.out.println(Thread.currentThread().getName() + " has value " + threadLocal.get()); try { Thread.sleep(2000); } catch (InterruptedException e) { } System.out.println(Thread.currentThread().getName() + " has value " + threadLocal.get()); } } public static void main(String[] args) throws InterruptedException { MyRunnable sharedRunnableInstance = new MyRunnable(); Thread thread1 = new Thread(sharedRunnableInstance, "Thread 1"); Thread thread2 = new Thread(sharedRunnableInstance, "Thread 2"); thread1.start(); thread2.start(); thread1.join(); //wait for thread 1 to terminate thread2.join(); //wait for thread 2 to terminate } }