同步静态方法和非静态方法之间的区别 [英] difference between synchronizing a static method and a non static method

查看:104
本文介绍了同步静态方法和非静态方法之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在java中同步静态方法和非静态方法有什么区别?任何人都可以用一个例子来解释。在同步方法和同步代码块方面是否有任何区别?

What is the difference between synchronizing a static method and a non static method in java?Can anybody please explain with an example. Also is there any difference in synchronizing a method and synchronizing a block of code?

推荐答案

我将尝试添加一个示例来制作这个特别清楚。

I will try and add an example to make this extra clear.

如前所述,Java中的synchronized是监控概念。将代码块标记为已同步时,请使用对象作为参数。当一个执行线程进入这样一个代码块时,它必须先等待,直到同一个对象上的同步块中没有其他正在执行的线程。

As has been mentioned, synchronized in Java is an implementation of the Monitor concept. When you mark a block of code as synchronized you use an object as a parameter. When an executing thread comes to such a block of code, it has to first wait until there is no other executing thread in a synchronized block on that same object.

Object a = new Object();
Object b = new Object();
...
synchronized(a){
    doStuff();
}
...
synchronized(b){
    doSomeStuff();
}
...
synchronized(a){
    doOtherStuff();
}

在上面的例子中,运行doOtherStuff()的线程将阻止另一个线程进入保护doStuff()的代码块。但是一个线程可以在没有问题的情况下进入doSomeStuff()周围的块,因为它在Object b上而不是Object a上同步。

In the above example, a thread running doOtherStuff() would block another thread from entering the block of code protecting doStuff(). However a thread could enter the block around doSomeStuff() without a problem as that is synchronized on Object b, not Object a.

在实例上使用synchronized修饰符时方法(非静态方法),它与具有this作为参数的同步块非常相似。因此在下面的示例中,methodA()和methodB()的行为方式相同:

When you use the synchronized modifier on an instance method (a non-static method), it is very similar to having a synchronized block with "this" as the argument. So in the following example, methodA() and methodB() will act the same way:

public synchronized void methodA() {
  doStuff();
}
...
public void methodB() {
    synchronized(this) {
        doStuff();
    }
}

请注意,如果你有一个方法C()没有同步且没有同步块的类,没有什么能阻止线程进入该方法,粗心的编程可以让该线程访问对象中的非安全代码。

Note that if you have a methodC() in that class which is not synchronized and does not have a synchronized block, nothing will stop a thread from entering that method and careless programming could let that thread access non-safe code in the object.

如果你有一个带有synchronized修饰符的静态方法,那么与一个带有 ClassName.class 的同步块作为参数实际上是相同的(如果你有一个该类的对象, ClassName cn = new ClassName(); ,您可以使用类访问该对象C = cn.getClass();

If you have a static method with the synchronized modifier, it is practically the same thing as having a synchronized block with ClassName.class as the argument (if you have an object of that class, ClassName cn = new ClassName();, you can access that object with Class c = cn.getClass();)

class ClassName {
  public void static synchronized staticMethodA() {
    doStaticStuff();
  }
  public static void staticMethodB() {
    synchronized(ClassName.class) {
      doStaticStuff();
    }
  }
  public void nonStaticMethodC() {
    synchronized(this.getClass()) {
      doStuff();
    }
  }
  public static void unSafeStaticMethodD() {
   doStaticStuff();
  }
}

所以在上面的例子中,staticMethodA()和staticMethodB ()以同样的方式行事。执行线程也将被阻止访问nonStaticMethodC()中的代码块,因为它正在同一个对象上同步。

So in the above example, staticMethodA() and staticMethodB() act the same way. An executing thread will also be blocked from accessing the code block in nonStaticMethodC() as it is synchronizing on the same object.

然而,重要的是要知道什么都不会停止正在执行的线程访问unSafeStaticMethodD()。即使我们说静态方法在Class对象上同步,也不意味着它同步对该类中方法的所有访问。它只是意味着它使用Class对象进行同步。仍然可以进行非安全访问。

However, it is important to know that nothing will stop an executing thread from accessing unSafeStaticMethodD(). Even if we say that a static method "synchronizes on the Class object", it does not mean that it synchronizes all accesses to methods in that class. It simply means that it uses the Class object to synchronize on. Non-safe access is still possible.

这篇关于同步静态方法和非静态方法之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆