java中静态同步方法有什么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21812396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:48:11  来源:igfitidea点击:

What is the use of static synchronized method in java?

javamultithreadingcore

提问by snehal

I have one question in my mind , I read static synchronized method locked on class object and synchronized method locks on current instance of an object.So Whats the meaning of locked on class object ?

我脑子里有一个问题,我读到静态同步方法锁定在类对象上,同步方法锁定在对象的当前实例上。那么锁定在类对象上的含义是什么?

Can anyone please help me on this topic ?

任何人都可以帮我解决这个话题吗?

采纳答案by dasblinkenlight

In general, synchronizedmethods are used to protect access to resources that are accessed concurrently. When a resource that is being accessed concurrently belongs to each instance of your class, you use a synchronizedinstance method; when the resource belongs to all instances (i.e. when it is in a staticvariable) then you use a synchronized staticmethod to access it.

通常,synchronized方法用于保护对并发访问的资源的访问。当同时访问的资源属于类的每个实例时,您使用synchronized实例方法;当资源属于所有实例时(即当它在一个static变量中时)然后你使用一个synchronized static方法来访问它。

For example, you could make a staticfactory method that keeps a "registry" of all objects that it has produced. A natural place for such registry would be a staticcollection. If your factory is used from multiple threads, you need to make the factory method synchronized(or have a synchronizedblock inside the method) to protect access to the shared staticcollection.

例如,您可以创建一个static工厂方法来保存它产生的所有对象的“注册表”。这种注册的自然场所将是一个static集合。如果您的工厂被多个线程使用,您需要创建工厂方法synchronized(或synchronized在方法内部有一个块)来保护对共享static集合的访问。

Note that using synchronizedwithout a specific lock object is generally not the safest choice when you are building a library to be used in code written by others. This is because malicious code could synchronize on your object or a class to block your own methods from executing. To protect your code against this, create a private "lock" object, instance or static, and synchronize on that object instead.

请注意,synchronized在构建要在其他人编写的代码中使用的库时,不使用特定的锁定对象通常不是最安全的选择。这是因为恶意代码可能会在您的对象或类上同步以阻止您自己的方法执行。为了保护您的代码不受此影响,请创建一个私有的“锁定”对象、实例或静态,然后在该对象上进行同步。

回答by Luis Pena

In simple words a static synchronizedmethod will lock the class instead of the object, and it will lock the class because the keyword staticmeans: "class instead of instance".

简单来说,static synchronized方法将锁定类而不是对象,并且它将锁定类,因为关键字的static意思是:“类而不是实例”。

The keyword synchronizedmeans that only one thread can access the method at a time.
And static synchronizedmean:

关键字synchronized表示一次只有一个线程可以访问该方法。
而且static synchronized意味着:

Only one thread can access the class at one time.

一次只有一个线程可以访问该类。

回答by Boann

At run time every loaded class has an instance of a Classobject. That is the object that is used as the shared lock object by static synchronizedmethods. (Any synchronized method or block has to lock on someshared object.)

在运行时,每个加载的类都有一个Class对象的实例。即被方法用作共享锁对象的对象static synchronized。(任何同步方法或块都必须锁定某个共享对象。)

You can also synchronize on this object manually if wanted (whether in a static method or not). These three methods behave the same, allowing only one thread at a time into the inner block:

如果需要,您还可以手动同步此对象(无论是否在静态方法中)。这三种方法的行为相同,一次只允许一个线程进入内部块:

class Foo {
    static synchronized void methodA() {
        // ...
    }

    static void methodB() {
        synchronized (Foo.class) {
            // ...
        }
    }

    static void methodC() {
        Object lock = Foo.class;
        synchronized (lock) {
            // ...
        }
    }
}

The intended purpose of static synchronizedmethods is when you want to allow only one thread at a time to use some mutable state stored in staticvariables of a class.

static synchronized方法的预期目的是当您希望一次只允许一个线程使用存储在static类变量中的某些可变状态时。

Nowadays, Java has more powerful concurrency features, in java.util.concurrentand its subpackages, but the core Java 1.0 constructs such as synchronizedmethods are still valid and usable.

如今,Java 具有更强大的并发特性,injava.util.concurrent及其子包,但核心 Java 1.0 构造(如synchronized方法)仍然有效和可用。

回答by Sarala Kumarage

static methods can be synchronized. But you have one lock per class. when the java class is loaded coresponding java.lang.class class object is there. That object's lock is needed for.static synchronized methods. So when you have a static field which should be restricted to be accessed by multiple threads at once you can set those fields private and create public static synchronized setters or getters to access those fields.

静态方法可以同步。但是每个班级都有一个锁。当加载 java 类时,相应的 java.lang.class 类对象就在那里。.static 同步方法需要该对象的锁。因此,当您有一个静态字段应该被限制为一次被多个线程访问时,您可以将这些字段设置为私有并创建公共静态同步 setter 或 getter 来访问这些字段。

回答by Aman Gupta

Suppose there are multiple static synchronized methods (m1, m2, m3, m4) in a class, and suppose one thread is accessing m1, then no other thread at the same time can access any other static synchronized methods.

假设一个类中有多个静态同步方法(m1、m2、m3、m4),并且假设一个线程正在访问m1,那么同时没有其他线程可以访问任何其他静态同步方法。

回答by Ashutosh

Java VM contains a single class object per class. Each class may have some shared variables called static variables. If the critical section of the code plays with these variables in a concurrent environment, then we need to make that particular section as synchronized. When there is more than one static synchronized method only one of them will be executed at a time without preemption. That's what lock on class object does.

Java VM 每个类包含一个类对象。每个类可能有一些称为静态变量的共享变量。如果代码的关键部分在并发环境中使用这些变量,那么我们需要使该特定部分同步。当有多个静态同步方法时,一次只会执行其中一个方法而不会被抢占。这就是锁定类对象的作用。