首页 \ 问答 \ Lock.tryLock()的用例(Use case for Lock.tryLock())

Lock.tryLock()的用例(Use case for Lock.tryLock())

在方法tryLock()Lock接口的API文档中,粘贴了此代码示例,

此方法的典型用法习惯是:

  Lock lock = ...;
  if (lock.tryLock()) {
      try {
          // manipulate protected state
      } finally {
          lock.unlock();
      }
  } else {
      // perform alternative actions
  }

我的问题是,这个用例在Java 5之前是不存在的还是人们通过其他技术来实现它?

我无法理解是否需要根据锁定可用性执行替代操作

有人可以为此解释实际用例吗?

我确信这种技术不是直接替代synchronized写无死锁代码。


At API docs of Lockinterface for method tryLock(), this code sample is pasted,

A typical usage idiom for this method would be:

  Lock lock = ...;
  if (lock.tryLock()) {
      try {
          // manipulate protected state
      } finally {
          lock.unlock();
      }
  } else {
      // perform alternative actions
  }

My question is , does this use case not existed before Java 5 or folks used to implement it via some other techniques?

I am not able to comprehend the need to execute perform alternative actions based on lock availability.

Can somebody please explain real use cases for this?

I am sure this technique is not a straight forward replacement of synchronizedto write deadlock free code.


原文:https://stackoverflow.com/questions/41788074
更新时间:2023-11-10 17:11

最满意答案

类名中的“_”用于表示目录结构。 您已将目录“helpers”重命名为“helper”: application/classes/helper/


The "_" in the class name is used to represent the directory structure. You have the rename the directory "helpers" into "helper": application/classes/helper/

相关问答

更多
  • 添加echo $this->template; 到视图结束时工作。 它不在Kohana或KSmarty文档/示例中,但它足够接近我。 如果有其他人想出一个没有echo解决问题的答案,我会将答案标记为已接受,但在此之前,我有一个解决方案。
  • 我能看到该功能失败的唯一地方是你没有提供cookie盐 。 确保你设置一个,看看这是否修复了你的错误。 在引导程序中: // Use a long and random value. Cookie::$salt = '' The only place where I could see that function failing is if you have failed to provide a cookie salt. Make sure you set one and see if this fix ...
  • martswite说的只适用于2. *版本。 不推荐在3. *版本中关闭错误标志,因为您会得到正常的PHP错误。 我的问题是:如果我禁用了Kohana的错误处理,那么存在破坏业务逻辑的风险? 如果你使用try catch来查看MySQL是否连接,那么是的。 如果是这样,为什么Kohana可以禁用? 这是我已经给开发者的东西( http://dev.kohanaframework.org/issues/4017 )。 我没有看到任何理由让它在3. *版本中出现,所以你并不是唯一一个感到困惑的人。 根据你的这种 ...
  • 你可以在Github上找到3.1模块: Jelly for 3.1 (非官方分支,Banks和Jonathan Geiger没有时间进行开发) 小枝分支为3.1 You can find 3.1 modules at Github: Jelly for 3.1 (unofficial fork, Banks and Jonathan Geiger have no time for development) Sprig branch for 3.1
  • Kohana API应该有所帮助。 基本步骤是: 创建验证对象( $array = Validation::factory($_FILES); ) 定义规则( $array->rule('file', 'Upload::not_empty')->rule('file', 'Upload::type', array(array('jpg', 'png', 'gif'))); ) 检查文件( if ($array->check()) ) 并保存( $filepath = Upload::save($_FILES ...
  • 2之间有一个微妙的区别: 在创建和销毁实例时,将调用constructor和destructor 。 before执行控制器的操作之前和之后调用before和after方法。 除此之外,您无法保证在执行操作之前和之后调用constructor和destructor ,但您可以保证对于before和after方法。 There's a subtle difference between the 2: The constructor and destructor are called when an insta ...
  • 将任务文件名从demo.php更改为Demo.php 。 第一个字母应该大写。 某些操作系统区分大小写,因此它可能适用于您的本地计算机,但不适用于您的服务器。 Change the task file name from demo.php to Demo.php. First letter should be capitalized. Some OS's are case sensitive, so it may works on your local machine, but not on your se ...
  • 根据Kohana 3.3约定 ,类映射的行为如下: media_Core映射到application/classes/media/Core.php Helpers_Media映射到application/classes/Helpers/Media.php 约定页面还描述了命名类的样式指南。 如果您希望调用media::alert() ,则必须将您的类命名为media并在application/classes/media.php 。 As per the Kohana 3.3 conventions the ...
  • 类名中的“_”用于表示目录结构。 您已将目录“helpers”重命名为“helper”: application/classes/helper/ The "_" in the class name is used to represent the directory structure. You have the rename the directory "helpers" into "helper": application/classes/helper/
  • 你可以在bootstrap中更改它: Kohana::$log->attach(new Log_File(APPPATH.'logs')); You can change it in bootstrap: Kohana::$log->attach(new Log_File(APPPATH.'logs'));

相关文章

更多

最新问答

更多
  • 获取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的基本操作命令。。。