Java:我们应该尽快退出try-with-resource块以释放资源吗?(Java: Should we exit from try-with-resource block ASAP to release the resource?)
以下哪个代码段更好? 在处理资源方面更好。
try (Jedis jedis = jedisPool.getResource()) { String value = jedis.get("key"); // Validation calls using `value` but not using `jedis` // Another DB call using `value` but not using `jedis` }
要么
String value; try (Jedis jedis = jedisPool.getResource()) { value = jedis.get("key"); } // Validation calls using `value` but not using `jedis` // Another DB call using `value` but not using `jedis`
在第一个代码片段中,资源一直保持到其他不相关的操作完成,而在第二个代码片段中,它会在使用后立即释放?
Which of the below code snippet is better? Better in terms of handling resource.
try (Jedis jedis = jedisPool.getResource()) { String value = jedis.get("key"); // Validation calls using `value` but not using `jedis` // Another DB call using `value` but not using `jedis` }
OR
String value; try (Jedis jedis = jedisPool.getResource()) { value = jedis.get("key"); } // Validation calls using `value` but not using `jedis` // Another DB call using `value` but not using `jedis`
In the first code snippet, the resource is held up until the other unrelated operations are completed whereas in the second it is being released as soon as it is used?
原文:https://stackoverflow.com/questions/51515492
最满意答案
那应该实际上应该在循环内部,就像
$args = array('post_type' => 'event') // setup my custom post type $todaysdate = "blah blah"; //setup for today's date // the wp loop query_posts($args); if ( (have_posts()) : while (have_posts()) : the_post(); $eventdate = "blah blah"; // setup for the date of the event; if($eventdate >= $todaysdate)) echo $event; endif; endwhile; endif;
编辑:
Thanks, this works fine, however, I want to output a set number of events (5 in total) which I've defined in $args. So $args sets up 5 events, but the second IF then filters out old events so I end up with less than 5.
但这就是你在第一时间拥有的原因。 但是,如果您的意思是它应该显示在该日期之后的5个事件,则需要修改
wp_query
本身。 见这里的例子That if should actually be inside the loop, like
$args = array('post_type' => 'event') // setup my custom post type $todaysdate = "blah blah"; //setup for today's date // the wp loop query_posts($args); if ( (have_posts()) : while (have_posts()) : the_post(); $eventdate = "blah blah"; // setup for the date of the event; if($eventdate >= $todaysdate)) echo $event; endif; endwhile; endif;
EDIT:
Thanks, this works fine, however, I want to output a set number of events (5 in total) which I've defined in $args. So $args sets up 5 events, but the second IF then filters out old events so I end up with less than 5.
But that's the very same reason you have that if in first place. However if you mean that it should show 5 events that are after that date then the
wp_query
itself needs to be modified. See Examples Here
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
感谢Mark Schultheiss带领我回答这个问题。 我不是100%,这是做事情的正确方式,但现在可能会通过/失败。 function checkIfDescending(){ const accountNumbersDescending = pa_search.account_no_column_elements(); let previousDescendingValue = ''; let isDescending = true; ...
-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
在设置循环之前,在循环外设置变量的最佳方法是什么?(What's the best way to set a variable outside a loop before setting up the loop?)[2023-03-21]
那应该实际上应该在循环内部,就像 $args = array('post_type' => 'event') // setup my custom post type $todaysdate = "blah blah"; //setup for today's date // the wp loop query_posts($args); if ( (have_posts()) : while (have_posts()) : the_post(); $eventdate = "blah ... -
您需要使用map和marker来打破事件处理程序所具有的闭包。 google.maps.event.addListener(marker, 'click', (function(lmap, lmarker) { return function() { infoWindow.open(lmap, lmarker); } })(map, marker) ); 你拥有它的方式,事件处理程序没有创建一个将 map和marker的值传递给inforWindow.open的函数,它正在传递实际 ...
-
我认为问题是你使用的is 。 试试这段代码: for comment in comments: for comment_user in comment_users: if comment_user['_id'] == comment['created_by']: comment.user_display_name = comment_user['display_name'] break 当(错误地)使用is比较string对象时会发生 ...
-
如果for /d %%a in ("F:\backup*") do的循环没有检测到名称以backup开头的任何目录,则它们要么不存在,要么隐藏属性和/或系统集。 要检测此类隐藏或系统目录,请使用以下命令替换for /D循环: rem Change to parent directory "F:\" temporarily in order for the `~f` modifier to resolve the full path properly: pushd "F:\" || exit /B 1 for ...
-
将变量值设置为for循环(Setting variable value to for loop)[2022-08-15]
您可以使用/l开关轻松地批量使用for循环来实现couting循环,如示例所示。 这些可以像这样嵌套: @echo off for /l %%w in (1, 1, 10) do ( for /l %%x in (1, 1, 10) do ( for /l %%y in (1, 1, 10) do ( for /l %%z in (1, 1, 10) do ( echo currently at v%%w.%%x.%%y.%%z ) ) ) ) ... -
Ruby:如果变量设置则运行循环(Ruby: Run loop if variable set)[2023-06-04]
您可以使用Proc - Ruby代码块的具体化版本。 将命令放在proc中,然后将proc传递给timeout方法(使用&运算符将其作为块而不是普通参数传递)或者直接call它。 例: block = proc do # this is the code I want to run # with or without the timeout stdout, stdeerr, status = Open3.capture3(cmd) @output = stdout @result = ... -
如果要设置全局变量,请设置_G["damage"..i] 。 如果你想设置局部变量,那你就不走运了。 考虑设置damage[i] 。 If you want to set global variables, set _G["damage"..i]. If you want to set local variables, you're out of luck. Consider setting damage[i] instead.