静态和非静态方法的同步块
我创建了两个线程,并使用称为该对象的静态和非静态方法的单个类实例。理想情况下,静态方法需要使用类名来调用,我也这样做了。
我同步了线程正在调用其方法的类的私有静态成员上的静态和非静态方法。我注意到输出是同步的!
我的问题是:
静态方法如果使用同步块进行同步,通常需要类实例,那么它如何接受静态对象!
当调用静态方法的线程获取类级锁和调用非静态方法的线程获取对象级锁时,输出是如何同步的!
当
尽管我在基于对象的静态和非静态方法中都使用了同步块,但它确实不应该同步;还是静态对象上的同步块的特殊情况?
请告诉我。
以下是我写的代码:
public class StaticNonStaticSynch
{
public static void main(String[] args)
{
final StaticNonStaticTest staticNonStaticTest = new StaticNonStaticTest();
Runnable runnable1 = new Runnable()
{
@Override
public void run()
{
staticNonStaticTest.nonStaticMethod();
}
};
Runnable runnable2 = new Runnable()
{
@Override
public void run()
{
staticNonStaticTest.staticMethod();
}
};
Thread thread1 = new Thread(runnable1, "First Thread");
Thread thread2 = new Thread(runnable2, "Second Thread");
thread1.start();
thread2.start();
}
}
class StaticNonStaticTest
{
private static Object object = new Object();
void nonStaticMethod()
{
synchronized (object)
{
for(int i=0;i<500;i++)
{
System.out.println("Non - Static method called by " + Thread.currentThread().getName() +" : = "+i);
}
}
}
static void staticMethod()
{
synchronized (object)
{
for(int i=0;i<500;i++)
{
System.out.println("Static method called by " + Thread.currentThread().getName() +" : = "+i);
}
}
}
}
I created two threads and using a single instance of class called the static and non-static methods of that object. Ideally static methods need to be called using the class name and i did that too.
I synchronized both the static and non-static methods on a private static member of the class whose methods the threads are calling. I noticed that the output was synchronized!
My questions are:
Static methods if synchronized using a synchronized block it usually requires the class instance, then how did it accept a static object!
How was the output synchronized as threads calling static methods acquire the class level lock and threads calling non-static methods acquire object level lock!
Even though i have used synchronized block in both the static and non-static methods based on an object, it really shouldn't synchronize; or is it special case with synchronized blocks on static objects?
Please let me know.
Following is the code i have written:
public class StaticNonStaticSynch
{
public static void main(String[] args)
{
final StaticNonStaticTest staticNonStaticTest = new StaticNonStaticTest();
Runnable runnable1 = new Runnable()
{
@Override
public void run()
{
staticNonStaticTest.nonStaticMethod();
}
};
Runnable runnable2 = new Runnable()
{
@Override
public void run()
{
staticNonStaticTest.staticMethod();
}
};
Thread thread1 = new Thread(runnable1, "First Thread");
Thread thread2 = new Thread(runnable2, "Second Thread");
thread1.start();
thread2.start();
}
}
class StaticNonStaticTest
{
private static Object object = new Object();
void nonStaticMethod()
{
synchronized (object)
{
for(int i=0;i<500;i++)
{
System.out.println("Non - Static method called by " + Thread.currentThread().getName() +" : = "+i);
}
}
}
static void staticMethod()
{
synchronized (object)
{
for(int i=0;i<500;i++)
{
System.out.println("Static method called by " + Thread.currentThread().getName() +" : = "+i);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在全球范围内,存在一个名为
StaticNonStaticTest.object
的实例。每当您在该事物上进行同步时(无论来自何处),您都会在同一个锁上进行同步。Globally, there exists one instance of the thing called
StaticNonStaticTest.object
. Whenever you synchronize on that thing (regardless where from), you are synchronizing on the same lock.您正在基于静态对象进行同步,这就是在类级别获取锁并且静态和非静态方法都同步的原因。
如果您注释掉静态和非静态方法中的“同步(对象)”行,您可以看到调用不再同步。
You are synchronizing on the basis of a static object,that's the reason the lock is obtained at the class level and both the static and non-static method's are synchronized.
If you comment out the line "synchronized (object)" in the static and non static method's you can see that the calls are no longer synchronized.