import java.util.concurrent.*;

public class ThreadLocalStorage {
    public static void main (String[] args) throws Exception {
        new ThreadLocalStorage().main();
    }
    
    private void main() throws Exception {
        Thread[] T = new Thread[10];
        
        for (int k=0; k < T.length; k++) {
            T[k] = new MyThread();
        }
            
        for (int k=0; k < T.length; k++) {
            T[k].start();
        }
            
        for (int k=0; k < T.length; k++) {
            T[k].join();
        }
            
        System.out.println("<enter> to exit");
        System.in.read();
    }
    
    private static long tid() {
        return Thread.currentThread().getId(); 
    }

    private static final ThreadLocal<Long> 
        tli = new ThreadLocal<Long>() {
            @Override 
            protected Long initialValue() {
                return tid() + 1000;
            }
        };

    class MyThread extends Thread {
        public void run() {
            System.out.format("%d %d %n", tid(), tli.get().intValue());
        }
    }
}
