class Bank { ThreadLocal<Integer> t = new ThreadLocal<Integer>() { @Override protected Integer initialValue() { return 100; } }; public void set() { t.set(t.get() + 10); } public int get() { return t.get(); } } class Transer implements Runnable { Bank bank; public Transer(Bank bank) { this.bank = bank; } @Override public void run() { for (int i = 0; i < 10; i++) { bank.set(); System.out.println(Thread.currentThread() + "" + bank.get()); } } } public class FF { public static void main(String[] args) throws Exception { Bank b = new Bank(); Transer t = new Transer(b); Thread t1 = new Thread(t); t1.start(); Thread t2 = new Thread(t); t2.start(); // thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程。 t1.join(); t2.join(); System.out.println(b.get()); } }