首页 \ 问答 \ 为什么隐式锁没有发生死锁?(Why deadlock is not happening for implicit locks?)

为什么隐式锁没有发生死锁?(Why deadlock is not happening for implicit locks?)

什么是可重入锁定和概念?

如果一个锁是不可重入的,你可以抓住锁,然后当你再次抓住它时阻塞,有效地使你自己的进程陷入僵局。

public class ImplicitLock {

synchronized public void test1() {
    System.out.println("enter test1");
    test2();
    System.out.println("exit test1");

}

synchronized public void test2() {
    System.out.println("inside test2");

}

}

从主班,我执行

ImplicitLock lock1 = new ImplicitLock();
lock1.test1();

我得到了以下输出虽然我期待死锁,当按照SO隐式锁定描述调用test2但它没有

enter test1
inside test2
exit test1

What is the Re-entrant lock and concept in general? says

If a lock is non re-entrant you could grab the lock, then block when you go to grab it again, effectively deadlocking your own process.

public class ImplicitLock {

synchronized public void test1() {
    System.out.println("enter test1");
    test2();
    System.out.println("exit test1");

}

synchronized public void test2() {
    System.out.println("inside test2");

}

}

From main class , i executed

ImplicitLock lock1 = new ImplicitLock();
lock1.test1();

I got the below output though i was expecting deadlock when call goes for test2 as per SO implicit lock description but it didn't

enter test1
inside test2
exit test1

原文:https://stackoverflow.com/questions/29183510
更新时间:2023-08-28 13:08

最满意答案

插件应该如何知道它是应用程序foo的一部分而不是应用程序bar ? 如果您不希望插件成为foo命名空间的一部分,则只需将处理程序附加到根记录器,就不会收到“无法找到处理程序”的消息。


How is the plugin supposed to know whether it's part of application foo as opposed to application bar? If you don't want your plugins to be part of the foo namespace, you can just attach your handlers to the root logger and you won't get the message about "no handlers could be found".

相关问答

更多
  • 似乎这个问题与父目录中的模块无关, 您需要将包含ptdraft的目录添加到PYTHONPATH 你说import nib与你一起工作,这可能意味着你将ptdraft本身(而不是它的父)添加到PYTHONPATH。 It seems that the problem is not related to the module being in a parent directory or anything like that. You need to add the directory that contains ...
  • 这里有几件事简化了我的测试,对我来说工作得非常好。 总是从项目根目录进行测试 起初我假设,我的测试用例应该可以从任何目录中使用。 事实上,没有理由以这种方式使测试用例变得灵活,并且决定从项目根运行所有测试只能极大地简化解决方案。 这对许多程序员来说可能是显而易见的,但对我来说,这是向简化测试迈出的重要一步。 保持测试目录与软件包代码分离 将生产代码与测试代码混合似乎很合理,但很快就会变得混乱。 最后,我决定在项目中使用单独的tests (复数)目录,它对我来说工作得非常好。 优点是: 测试“接近”选择(参见 ...
  • 插件应该如何知道它是应用程序foo的一部分而不是应用程序bar ? 如果您不希望插件成为foo命名空间的一部分,则只需将处理程序附加到根记录器,就不会收到“无法找到处理程序”的消息。 How is the plugin supposed to know whether it's part of application foo as opposed to application bar? If you don't want your plugins to be part of the foo namespac ...
  • 现代相对导入( 这里是参考 )是与包相关的和特定于包的,因此只要包的内部结构没有改变,您就可以将包整体移动到任何您想要的位置。 虽然Joran Beasley的答案也应该起作用(虽然在那些绝对导入不是默认值的旧版Python中似乎没有必要,因为首先在包的目录中检查旧的导入样式),我个人并不喜欢修改导入路径,就像你不需要那样,特别是如果你需要加载模块或包现在影子的其他一些软件包或模块。 但是,警告:这些确实需要将相关模块作为包的一部分加载,或者至少设置其__name__以指示包中的位置。 当__name__ ...
  • 这不是python如何工作,如果你想在模块中使用numpy库(在这种情况下在eplib模块中),你需要在该模块中导入它, eplib将不会在你的compare.py导入numpy模块。 你应该在eplib.py导入numpy - import numpy as np 我不认为效率会有任何问题,因为一旦python第一次导入模块,它会将模块缓存在sys.modules ,因此每当你重新导入它时(即使它在不同的模块中)因为它是相同的python进程,Python不会重新导入它,而是从sys.modules返回 ...
  • 使用显式相对导入: from . import subapi Use an explicit relative import: from . import subapi
  • 您还需要在test2.py导入Parent模型 from test1 import Parent class Child(Parent): def eggs(self): print "something else" You need to import the Parent model in test2.py as well from test1 import Parent class Child(Parent): def eggs(self): pr ...
  • 函数能够将某些内容返回到调用它们的位置。 它称为返回值:p def import_something(): # decide what to import # ... mod = __import__( something ) return mod my_module = import_something() my_module.do_stuff() 好风格,没有麻烦。 关于你的更新,我想你可以为你添加这样的东西__init__.py : import os # make ...
  • 您可以创建专用的python脚本文件,仅用于设置日志记录。 基本上,该文件将包含您的ABC.py文件现在包含的内容。 然后只需在每个可能是“入口点”的脚本中导入此文件。 即使文件导入两次,文件中的代码也只运行一次。 You could create a dedicated python script file just for setting up the logging. Basically, that file would contain exactly what your ABC.py file co ...
  • 这是一个简单的例子: foo.py : import numpy as np def foo(x): """Return 2D square array of zeros.""" return np.zeros((x, x)) del np bar.py : import numpy as np def bar(): """Return 3x3 square array.""" return np.arange(9).reshape(3, 3) main.py : ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。