简体   繁体   English

多线程使用的Java同步和静态同步方法

[英]Java Synchronized and static synchronized method used by Multiple Threads

I get confused with the following scenario all the time 我一直对以下情况感到困惑

There is only one object of MyClass and there are two threads T1, T2. MyClass只有一个对象,并且有两个线程T1,T2。 Now one thread say T2 will be able to use the synchronized method m1() which is having the only MyClass object lock and other thread T2 will get blocked if it will try to access m1(). 现在有一个线程说T2将能够使用同步方法m1(),该方法具有唯一的MyClass对象锁,而其他线程T2如果尝试访问m1()则将被阻塞。

Now my perception is that if T2 will try to access static synchronized method m2() by accessing static shared field it will get blocked as current object lock is with T1 and wont be able to execute m2() and if there were two objects of Myclass then T2 thread would be able to access m1(). 现在我的理解是,如果T2将尝试通过访问静态共享字段来访问静态同步方法m2(),则它将被阻塞,因为当前对象锁定在T1上,并且将无法执行m2(),并且如果MyClass有两个对象那么T2线程将能够访问m1()。 Am I correct or wrong? 我是对还是错?

class MyClass
{

            public static int i = 5;

        public synchronized void m1()
        {
                System.out.println(i); //uses static field i of MyClass
            //T1 is executing this method
        }

            public static synchronized void m3()
            {
                //T2 will be able to call this method on same object lock while it is using
                //static field i???
                System.out.println(i);//uses static field i of MyClass
            }
}

This is very confusing please help. 这非常令人困惑,请帮忙。 Thanks in advance. 提前致谢。

Not correct. 不正确。 Since m2 is not synchronized no thread will be blocked when attempting to invoke it no matter the state of the synchronized method m1 . 由于m2synchronized ,因此无论synchronized方法m1处于什么状态,尝试调用它时都不会阻塞任何线程。

Acquiring a lock on an object doesn't block access to that object, it just block the ability for another thread to acquire the lock on the same object concurrently. 获取对象上的锁不会阻止对该对象的访问,而只是阻止另一个线程同时获取同一对象上的锁的能力。 If the other thread does not attempt to acquire the lock, it will not be blocked. 如果另一个线程不尝试获取该锁,则不会被阻塞。

Now my perception is that if T2 will try to access non synchronized method m2() it will get blocked as current object lock is with T1 现在我的理解是,如果T2尝试访问非同步方法m2(),它将被阻塞,因为当前对象锁定在T1上

No. m2() isn't synchronized, therefore there's nothing to block it. m2()未同步,因此没有阻止它的内容。 That's the difference between a synchronized and a non-synchronized method. 这就是同步方法和非同步方法之间的区别。

When a thread tries to enter a synchronized method, it will block until it can acquire the appropriate monitor (lock). 当线程尝试输入同步方法时,它将阻塞直到可以获取适当的监视器(锁定)。 It will release the lock when it exits the method. 退出该方法时,它将释放锁定。 Neither of those steps occurs for a non-synchronized method. 对于非同步方法,这些步骤均不会发生。

(As an aside, I would personally suggest not using synchronized methods at all, for the most part. Instead, synchronize within the method, on a reference which is only known about within that class. That makes it easier to reason about the code, as only your class will be able to acquire that lock.) (顺便说一句,我个人建议大多数情况下完全不使用同步方法。相反,在该类内是已知的引用上该方法进行同步。这样可以更轻松地进行代码推理,因为只有您的班级才能获得该锁。)

You're wrong. 你错了。 Objects aren't "locked" by synchronized blocks, the only thing that happens is that other synchronized blocks trying to acquire the monitor for that object have to wait. 对象不会被synchronized块“锁定”,唯一发生的事情是,其他试图获取该对象监视器的synchronized块必须等待。

To sum it up: synchronized has absolutely no blocking effect on non-synchronized code. 综上所述: synchronized对非同步代码绝对没有阻塞作用。

Thread acquires a lock when it enters a synchronized method /block. 线程进入同步方法/块时将获得锁。 as m2() is not synchronized, it will be executed by thread 2. remember that, Locks only come into picture when there are synchronized methods 由于m2()不同步,它将由线程2执行。请记住,只有在存在同步方法时,锁才会出现

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

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