【发布时间】:2014-09-03 11:01:50
【问题描述】:
这是否意味着任何线程,无论它获得的对象如何,都不会干扰其他线程在同步静态方法中执行。即使我们用 class_name.static_Method 调用。
Ex- If we have two thread :
public class Test implements Runnable {
public synchronized static testMethod(){}
public void run(){ testMethod(); }
public static void main(String[] args) {
Test obj1=new Test();
Test obj2=new Test();
Thread t1=new Thread(obj1);
Thread t2=new Thread(obj2);
t1.start(); // thread on object obj1
t2.start(); // Thread on object obj2
Test.testMethod(); // main thread
}
}
如果线程 t1 进入静态方法,那么 t2 和主线程将不会进入该方法,即使它们有不同的对象。 如果我错了,请纠正我。
【问题讨论】:
标签: java multithreading static locking