首页 \ 问答 \ try-with-resource是否有可能无法关闭资源?(Is it possible for try-with-resource to fail to close resources?)

try-with-resource是否有可能无法关闭资源?(Is it possible for try-with-resource to fail to close resources?)

根据Oracle文档

try-with-resources语句是一个声明一个或多个资源的try语句。 资源是一个对象,必须在程序完成后关闭它。 try-with-resources语句确保在语句结束时关闭每个资源。 实现java.lang.AutoCloseable的任何对象(包括实现java.io.Closeable的所有对象)都可以用作资源

如果资源没有实现AutoCloseable,它不能在try块中声明,它必须在body部分声明并且必须在finally块中显式关闭。 try-with-resource是否有可能无法清理资源,除非您使用不正确的习惯用法,如果有嵌套资源并且close()调用对底层资源不是幂等的? 我理解的是try-with-resource只能确保在场景后面调用close(),但如果close本身不关闭底层资源,try-with-resource将无法清理资源。 有没有人有相反的观点或更多的澄清?


As per Oracle documentation

The try-with-resources statement is a try statement that declares one or more resources. A resource is as an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource

If a resource does not implement AutoCloseable it cannot be declared within try block, it has to be declared in the body part & has to be explicitly closed in finally block. Is it possible for try-with-resource to fail to clean up resources unless you use the idiom incorrectly like if there are nested resources & close() call is not idempotent for the underlying resource? What I understand is try-with-resource only ensures close() getting called behind the scene, but if close itself does not close underlying resources try-with-resource will remain ineffective to clean-up resources. Does anyone have a contrary view or more clarification?


原文:https://stackoverflow.com/questions/36831233
更新时间:2023-08-16 12:08

最满意答案

您可以依赖于相同的href值,并根据该值确定元素

$('.tabs-menu a[href="' + href + '"]').parent().addClass("current");
$('.tabs-menu a').not('a[href="' + href + '"]').parent().removeClass("current");

这是一个工作小提琴

http://jsfiddle.net/9mqf8ar0/2/


You can lean on the same href value, and identify the elements based on that, something like

$('.tabs-menu a[href="' + href + '"]').parent().addClass("current");
$('.tabs-menu a').not('a[href="' + href + '"]').parent().removeClass("current");

here is a working fiddle

http://jsfiddle.net/9mqf8ar0/2/

相关问答

更多
  • 给你写了一个,看是不是理想中的效果,不对可以改: ps:看的时候记得改一下引用jquery路径 ---------------------------- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" >& ...
  • 这是您的问题的快速解决方案。 我手动创建select元素,并将更改委托给实际链接。 因此,无论您使用select导航,都会执行任何代码。 我在开始时创建了select元素,并使用媒体查询根据浏览器宽度显示/隐藏它 CSS #sidebar > .tabs-menu{display:block} #sidebar > select{display:none;} @media (max-width:400px){ #sidebar > .tabs-menu{display:none} #sid ...
  • 您可以依赖于相同的href值,并根据该值确定元素 $('.tabs-menu a[href="' + href + '"]').parent().addClass("current"); $('.tabs-menu a').not('a[href="' + href + '"]').parent().removeClass("current"); 这是一个工作小提琴 http://jsfiddle.net/9mqf8ar0/2/ You can lean on the same href value, a ...
  • $('.tab_contents').hide(); var count = 0; $('.tab').click(function() { count++; //increment count everytime it is clicked var target = $(this.rel); $('.tab_contents').not(target).hide(); target.slideToggle(100); $('#tabs_container ...
  • 您可以将onfocus添加到这些字段,因此如果通过“tab”或单击来聚焦该输入,则将触发此事件。 $("#table_appl,#table_enfr,#table_det01,#table_det02,#table_det03,#table_det04,#table_det05,#table_datais").on( "focus", "input:text", function( event ) {}) you could add onfocus to those fields, so if that ...
  • 当你说$('。leftKanji')时,你引用该类的所有元素。 下面的代码将获取作为.leftTab元素的子元素的元素。 您可以将.children()更改为jQuery api中的任何一个遍历选择器,但我使用那个作为示例。 但重要的是使用$(this),因为它与悬停的元素相对应。 $(document).ready(function(){ $('.leftTab').hover( function(){ $(this).children(.leftKanji').css('displa ...
  • 您可以查看.appendTo()的JSON Cookie解决方案。 它将提供一种存储和检索最后活动选项卡的简单方法。 用法记录如下: $.cookie( string key, mixed value [, hash options ] ) 因此,您可以存储活动选项卡的索引: $.cookie( 'activeTab', '3' ); 然后在页面加载时检索它: $.cookie( 'activeTab' ); //returns the set value 那么让我们来看看如何实现它: $("#nav ...
  • 有很多插件可用于此: 这是一个例子 演示: http : //jsfiddle.net/LtmA5/1/ 资源: http://jquery.aamirafridi.com/jst/ There are numerous plugins available for this: Here's one example DEMO: http://jsfiddle.net/LtmA5/1/ Source: http://jquery.aamirafridi.com/jst/
  • 看来你必须使用jquery.cookie才能做到这一点。 我修改了你的代码如下: $(document).ready(function() { var cookieName = 'tab_of'; var cookieTab = 'tab'; var cookieOptions = {expires: 7, path: '/'}; $("#content div").hide(); //Hide all content $("#" + $.cook ...
  • 请改用.show() & hide() 。 例: ul{height: 40px;} li{float: left;padding: 5px;background-color: #ccc;color:#333;margin-left: 10px;list-style: none;} .panel{height:100px;width: 800px;margin: 0 auto 10px;background-color: #ccc;/*display: none;*/} .sh ...

相关文章

更多

最新问答

更多
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • 电脑高中毕业学习去哪里培训
  • 电脑系统专业就业状况如何啊?
  • IEnumerable linq表达式(IEnumerable linq expressions)
  • 如何在Spring测试中连接依赖关系(How to wire dependencies in Spring tests)
  • Solr可以在没有Lucene的情况下运行吗?(Can Solr run without Lucene?)
  • 如何保证Task在当前线程上同步运行?(How to guarantee that a Task runs synchronously on the current thread?)
  • 在保持每列的类的同时向数据框添加行(Adding row to data frame while maintaining the class of each column)
  • 的?(The ? marks in emacs/haskell and ghc mode)
  • 一个线程可以调用SuspendThread传递自己的线程ID吗?(Can a thread call SuspendThread passing its own thread ID?)
  • 延迟socket.io响应,并“警告 - websocket连接无效”(Delayed socket.io response, and “warn - websocket connection invalid”)
  • 悬停时的图像转换(Image transition on hover)
  • IIS 7.5仅显示homecontroller(IIS 7.5 only shows homecontroller)
  • 没有JavaScript的复选框“关闭”值(Checkbox 'off' value without JavaScript)
  • java分布式框架有哪些
  • Python:填写表单并点击按钮确认[关闭](Python: fill out a form and confirm with a button click [closed])
  • PHP将文件链接到根文件目录(PHP Linking Files to Root File Directory)
  • 我如何删除ListView中的项目?(How I can remove a item in my ListView?)
  • 您是否必须为TFS(云)中的每个BUG创建一个TASK以跟踪时间?(Do you have to create a TASK for every BUG in TFS (Cloud) to track time?)
  • typoscript TMENU ATagParams小写(typoscript TMENU ATagParams lowercase)
  • 武陟会计培训类的学校哪个好点?
  • 从链接中删除文本修饰(Remove text decoration from links)