1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
public class ThreadL1 { private static ThreadLocal<Integer> seqNum = new ThreadLocal<Integer>(){ public Integer initialValue(){ return 0; } };
public int getNextNum(){ System.out.println("____"+seqNum.get()); seqNum.set(seqNum.get()+1); return seqNum.get(); }
public static void main(String[] args) { ThreadL1 tl = new ThreadL1(); TestClient t1 = new TestClient(tl); TestClient t2 = new TestClient(tl); TestClient t3 = new TestClient(tl);
t1.start(); t2.start(); t3.start(); }
private static class TestClient extends Thread{ private ThreadL1 tl; public TestClient(ThreadL1 tl){ this.tl = tl; }
public void run(){ for (int i = 0; i<3 ;i++){ System.out.println("Thread["+Thread.currentThread().getName()+"]--->tl["+tl.getNextNum()+"]"); } } } }
|