简体   繁体   English

什么是类级别同步。如果该类被一个线程锁定在同一个其他线程,则可以访问该类的另一个方法

[英]what is the class level synchronization.If the class is locked by one thread at same other thread can access the other method of that class

There is a class EmployeeInfo it has a static synchronized method non static synchronized method 有一个类EmployeeInfo它有一个静态同步方法非静态同步方法

public class EmployeeInfo{
    public static synchronized void insert(){
        //Insert code
    }

    public synchronized void update(){
        //update code
    }

    public void delete(){
        //update code
    }
}

There are five threads A, B, C, D, and E. Every thread is running. 有五个线程A,B,C,D和E.每个线程都在运行。

A thread comes and accesses the method insert() of class EmployeeInfo . 一个线程来访问类EmployeeInfo的方法insert()

At the same time: 与此同时:

  • Now B thread come and try to access insert() method again - what will happen? 现在B线程再来尝试再次访问insert()方法 - 会发生什么?
  • Now C thread come and try to access update() - what will happen? 现在C线程来尝试访问update() - 会发生什么?
  • Now D thread come and try to access delete() - what will happen? 现在D线程来尝试访问delete() - 会发生什么?

Please explain the concept of class level synchronization following the above example. 请按照上面的例子解释类级同步的概念。

There are two separate locks involved here - one for the instance on which you call update , and one for the class itself. 这里涉及两个单独的锁 - 一个用于调用update实例 ,另一个用于类本身。 So thread B would be blocked until thread A had completed, but the other two threads would execute without blocking. 因此线程B将被阻塞,直到线程A完成,但其他两个线程将执行而不会阻塞。 (D isn't synchronizing on anything anyway.) (无论如何,D都没有同步。)

Your code is broadly equivalent to: 您的代码大致相当于:

public class EmployeeInfo{
    public static void insert(){
        synchronized (EmployeeInfo.class) {
            // Insert code
        }
    }

    public void update() {
        synchronized (this) {
            // Update code
        }
    }

    public void delete() {
        // Note: no synchronization
        // Delete code
    }
}

I don't completely understand your question, but it looks as if you might be asking this: 我不完全理解你的问题,但看起来好像你可能会这样问:

If the class is locked by one thread [can other threads access] that class? 如果该类被一个线程锁定[其他线程可以访问]该类吗?

Absolutely! 绝对! "Lock" is a deceptive word. “锁定”是一个欺骗性的词。 When you use the synchronized keyword to lock an object (including a class object) it does not prevent other threads from accessing or modifying the object. 当使用synchronized关键字来锁定对象(包括一个类对象)它不会阻止其他线程访问或修改的对象。

The only thing that synchronized does is it prevents two threads from synchronizing on the same object at the same time. synchronized的唯一功能是它可以防止两个线程同时在同一个对象上同步。 If you want to prevent two or more threads from operating on the same data at the same time, then it is your responsibility to ensure that every method that can access the data is synchronized on the same object when it does it. 如果要防止两个或多个线程同时对同一数据进行操作,那么您有责任确保每个可以访问数据的方法在同一个对象上同步。

Class level locking prevents multiple threads to enter in synchronized block in any of all available instances on runtime.As per example for insert method of class EmployeeInfo 类级锁定可防止多个线程在运行时的任何可用实例中的synchronized块中输入。如EmployeeInfo类的insert方法的示例

I created object of class EmployeeInfo . 我创建了EmployeeInfo类的对象。

EmployeeInfo e1=new EmployeeInfo(); EmployeeInfo e1 = new EmployeeInfo(); Thread A comes and access insert() method of e1. 线程A来访问e1的insert()方法。

Now again created a new object for class EmployeeInfo EmployeeInfo e2=new EmployeeInfo(); 现在再次为类EmployeeInfo创建一个新对象EmployeeInfo e2 = new EmployeeInfo(); Thread B comes and access insert() method of e2. 线程B来访问e2的insert()方法。

Now again created a new object for class EmployeeInfo EmployeeInfo e3=new EmployeeInfo(); 现在再次为类EmployeeInfo创建一个新对象EmployeeInfo e3 = new EmployeeInfo(); Thread C comes and access insert() method of e3. 线程C来访问e3的insert()方法。

Currently thread A has monitor for EmployeeInfo object thread B and C both can't access insert() method of EmployeeInfo object while i created different-2 instances for EmployeeInfo object e1,e2,e2,Because here lock is on class level. 当前线程A具有EmployeeInfo对象的监视器线程B和C都不能访问EmployeeInfo对象的insert()方法,而我为EmployeeInfo对象e1,e2,e2创建了不同的-2实例,因为这里的锁是在类级别上。 So my statement giving a perfect example to understand class level synchronization . 所以我的陈述给出了理解类级同步的完美示例。 "Class level locking prevents multiple threads to enter in synchronized block in any of all available instances on runtime". “类级锁定可防止多个线程在运行时的所有可用实例中的同步块中进入”。

Inspite of that if we use Object level syncronization then then lock is on only one instance.not for every object e2,e3,e4.So lock will be for only specific object which is locked by anyone thread. 尽管如此,如果我们使用对象级同步,那么锁只在一个实例上。对于每个对象e2,e3,e4都不是。所以锁定仅适用于被任何线程锁定的特定对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM