wawxy2008 2014-06-24 15:02
浏览 354
已采纳

Java静态方法的线程安全性问题

如果多个线程同时访问同一个静态方法,后一个线程传递的参数值会覆盖前一个线程传递的参数值吗?代码示例如下:
被访问的静态资源:
public class C {

public static void test(String[] value) throws InterruptedException{
Thread.sleep(5000);

System.out.println(Thread.currentThread().getId());
for(String v : value){
    System.out.println(v);
}

}
}

线程1:

public class A {

public static void main(String[] args) throws InterruptedException {
C.test(new String[]{"A","B", "C"});
}
}

线程2:

public class B {
public static void main(String[] args) throws InterruptedException {
C.test(new String[]{"D","E", "F"});
}
}

在线程1访问静态方法test并传递参数后,假设在执行中或执行之前,下一个线程2也访问了test方法并传递了新的参数,此时在线程1遍历参数时,会遍历到线程2传递的参数吗?

  • 写回答

5条回答

  • iteye_15645 2014-06-25 17:20
    关注

    局部变量是线程安全的! 都有自己运行的线程堆栈!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?