读书人

典型的线程保险

发布时间: 2012-10-10 13:58:11 作者: rapoo

典型的线程安全

public class A1 {public static ThreadLocal tl = new ThreadLocal(); static Integer a=1;public static void main(String[] args) {for (int i = 0; i < 3; i++) {new Thread() {public void run() {try {synchronized (a) {Thread.sleep(10);tl.set(Thread.currentThread().getName()+"-----"+a++);System.out.println(Thread.currentThread().getName()+"-----"+tl.get());//b.getTl().remove();}} catch (Exception e) {e.printStackTrace();}}}.start();}}}

?

读书人网 >编程

热点推荐