是否按方法或按类发生死锁? [英] Does deadlock happen per method or per class?

查看:77
本文介绍了是否按方法或按类发生死锁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,我有几个同步方法的类First.当线程锁定类First时,它是按方法还是按类锁定?例如,以下代码是否会发生死锁?

Immagine that I have Class First with several synchronised methods. When a thread locks the class First, does it lock per method or per class? For example does deadlock happen for the following code?

public class DeadLockQuestion {

    public static class First{
        public synchronized void a(){

        }
        public synchronized void b(){

        }
        public synchronized void c(){

        }   
        public synchronized void d(){

        }
        public synchronized void e(){

        }       
    }

    public static void main(String... args){
        First f = new First();

        //This code is run in Thread 1
        f.a();
        // End
        //This code is run in Thread 2 simultanously
        f.b();
        //End
        // We have also Threads 3 & 4 & 5 that invoke c,d and e simultanously

    }

}

推荐答案

您在Java中有两个锁.一种是对象锁定.另一个是Class Lock. 对象锁仅锁定对同步非静态功能的访问.和类锁仅锁定同步的静态函数. 对您来说,它是对象f上的对象锁.因此,对象f的所有同步非静态功能均被锁定. 由于所有线程都使用相同的对象f,因此一次只能有一个线程访问您的非静态函数a(), b(),.... 在此处一个>

You have got two locks in Java. One is Object lock. and the other is Class Lock. The object lock locks access to synchronized non-static functions only. and Class lock locks on synchronized static functions only. For you, its an object lock on object f. So all the synchronized non-static functions are locked for object f. Since all the Threads are using the same object f, Only one Thread will be able to access your non-static functions a(), b(),... at a time. Read more here

does deadlock happen for the following code?

不,在您的情况下不会发生.因为当一个Thread持有锁时,其他线程无法进入您的同步函数中.DeadLock happens due to resources.
您只有一个资源,即对象f.这里没有死锁的意义,因为类First不会锁定另一个对象并且不会发生循环锁定.死锁需要循环锁定

No, it won't happen in your case. Because While one Thread is holding the lock, Other threads can't get inside your synchronized function.DeadLock happens due to resources.
You have only one resource thats Object f. There's no point of a dead-lock here because the class First doesn't lock another object and no cyclic lock can happen. Deadlock requires a cyclic lock!

某些信息:

  1. Java中的同步保证没有两个线程可以执行 同时需要相同锁定的同步方法或 同时进行.
  2. synchronized关键字只能与方法和代码块一起使用. 这些方法或块可以是静态的,也可以是非静态的.
  3. 无论何时线程进入Java同步方法或阻止它 获取锁,并且每当它离开java同步方法或 阻止它释放锁.即使线程离开也将释放锁定 完成后或由于任何错误而导致同步方法或 例外.
  4. 静态同步和非静态同步都有可能 同步方法可以同时运行,也可以同时运行,因为 他们锁定在不同的对象上.
    有用的资源
    这里,和此处
  1. Synchronization in java guarantees that no two threads can execute a synchronized method which requires same lock simultaneously or concurrently.
  2. synchronized keyword can be used only with methods and code blocks. These methods or blocks can be static or non-static both.
  3. When ever a thread enters into java synchronized method or block it acquires a lock and whenever it leaves java synchronized method or block it releases the lock. Lock is released even if thread leaves synchronized method after completion or due to any Error or Exception.
  4. It’s possible that both static synchronized and non static synchronized method can run simultaneously or concurrently because they lock on different object.
    Useful Source Here, and here

这篇关于是否按方法或按类发生死锁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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