当前位置:编程学习 > 网站相关 >>

典型的线程安全

Java代码 
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(); 
        } 
    } 

作者“J2EE技术探讨”
 

补充:综合编程 , 安全编程 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,