首页 \ 问答 \ CyclicBarrier是否会在最后等待时自行重置?(Does CyclicBarrier reset itself for the last await?)

CyclicBarrier是否会在最后等待时自行重置?(Does CyclicBarrier reset itself for the last await?)

我倾向于CyclicBarrier ,我写了这个演示。

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import static java.util.concurrent.ThreadLocalRandom.current;
public class CyclicBarrierDemo {
    public static void main(final String[] args) {
        final int threads = 100;
        final CyclicBarrier barrier
            = new CyclicBarrier(threads, () -> System.out.println("tripped"));
        final int rounds = 5;
        for (int i = 0; i < threads; i++) {
            new Thread(() -> {
                    for (int j = 0; j < rounds; j++) {
                        try {
                            Thread.sleep(current().nextLong(1000L));
                            barrier.await();
                        } catch (InterruptedException | BrokenBarrierException e) {
                            e.printStackTrace(System.err);
                            return;
                        }
                    }
            }).start();
        }
    }
}

正如我预期的那样 ,该程序会打印五次tripped和退出。

tripped
tripped
tripped
tripped
tripped

我的问题是,当最后一个await()到达时, CyclicBarrier实例会自行重置? 那么输出是预期的吗? 我找不到任何相关的话。


I'm leaning about the CyclicBarrier and I wrote this demo.

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import static java.util.concurrent.ThreadLocalRandom.current;
public class CyclicBarrierDemo {
    public static void main(final String[] args) {
        final int threads = 100;
        final CyclicBarrier barrier
            = new CyclicBarrier(threads, () -> System.out.println("tripped"));
        final int rounds = 5;
        for (int i = 0; i < threads; i++) {
            new Thread(() -> {
                    for (int j = 0; j < rounds; j++) {
                        try {
                            Thread.sleep(current().nextLong(1000L));
                            barrier.await();
                        } catch (InterruptedException | BrokenBarrierException e) {
                            e.printStackTrace(System.err);
                            return;
                        }
                    }
            }).start();
        }
    }
}

The program prints five tripped and quits, as I expected.

tripped
tripped
tripped
tripped
tripped

My question is that the CyclicBarrier instance reset itself when the last await() arrived? So that the output is expected? I couldn't find any words for that.


原文:https://stackoverflow.com/questions/42525669
更新时间:2023-10-20 19:10

最满意答案

你不能使用Flexbox这样做,所以如果你不能使用脚本而你无法改变标记,你可以用float完成这个

ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

li {
  float: left;
  width: calc(33.333% - 10px);
  margin: 5px;
  height: 30px;
  background: lightgray;
}

/* for wider screens, such as desktop */
@media screen and (min-width: 768px) {
  li {
    width: calc(50% - 10px);
  }
}
Markup sample 1

<ul>
  <li></li>
  <li></li>
</ul>
<ul>
  <li></li>
</ul>
<ul>
  <li></li>
  <li></li>
  <li></li>
</ul>

<div style="clear: both"></div>

Markup sample 2

<ul>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
</ul>
<ul>
</ul>
<ul>
  <li></li>
</ul>


既然你明确说明你不能改变标记,但不能不使用脚本,这里就是一个

标记样本1

(function() {
  window.addEventListener('load', function() {
    var uls = document.querySelectorAll('ul');
    for (var i = 1; i < uls.length; i++) {
      var lis = uls[i].querySelectorAll('li');
      for (var j = 0; j < lis.length; j++) {
        uls[0].appendChild(lis[j]);
      }
      uls[i].innerHTML = ''; // make them empty so CSS rule kicks in
    }
  });
})();
ul:empty {
  display: none;
}

ul {
  display: flex;
  flex-wrap: wrap;
  list-style: none;
  margin: 0;
  padding: 0;
}

li {
  flex-basis: calc(33.333% - 10px);
  margin: 5px;
  height: 30px;
  background: lightgray;
}


/* for wider screens, such as desktop */

@media screen and (min-width: 768px) {
  li {
    width: calc(50% - 10px);
  }
}
<ul>
  <li></li>
  <li></li>
</ul>
<ul>
  <li></li>
</ul>
<ul>
  <li></li>
  <li></li>
  <li></li>
</ul>

标记样本2

(function() {
  window.addEventListener('load', function() {
    var uls = document.querySelectorAll('ul');
    for (var i = 1; i < uls.length; i++) {
      var lis = uls[i].querySelectorAll('li');
      for (var j = 0; j < lis.length; j++) {
        uls[0].appendChild(lis[j]);        
      }
      uls[i].innerHTML = ''; // make them empty so CSS rule kicks in
    }
  });
})();
ul:empty {
  display: none;
}

ul {
  display: flex;
  flex-wrap: wrap;
  list-style: none;
  margin: 0;
  padding: 0;
}

li {
  flex-basis: calc(33.333% - 10px);
  margin: 5px;
  height: 30px;
  background: lightgray;
}


/* for wider screens, such as desktop */

@media screen and (min-width: 768px) {
  li {
    width: calc(50% - 10px);
  }
}
<ul>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
</ul>
<ul>
</ul>
<ul>
  <li></li>
</ul>


You can't do that using Flexbox, so if you can't use a script and you can't change markup, you can accomplish this with float

ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

li {
  float: left;
  width: calc(33.333% - 10px);
  margin: 5px;
  height: 30px;
  background: lightgray;
}

/* for wider screens, such as desktop */
@media screen and (min-width: 768px) {
  li {
    width: calc(50% - 10px);
  }
}
Markup sample 1

<ul>
  <li></li>
  <li></li>
</ul>
<ul>
  <li></li>
</ul>
<ul>
  <li></li>
  <li></li>
  <li></li>
</ul>

<div style="clear: both"></div>

Markup sample 2

<ul>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
</ul>
<ul>
</ul>
<ul>
  <li></li>
</ul>


Since your clearly stated you can't change markup, but not not using script, here is one with that

Markup sample 1

(function() {
  window.addEventListener('load', function() {
    var uls = document.querySelectorAll('ul');
    for (var i = 1; i < uls.length; i++) {
      var lis = uls[i].querySelectorAll('li');
      for (var j = 0; j < lis.length; j++) {
        uls[0].appendChild(lis[j]);
      }
      uls[i].innerHTML = ''; // make them empty so CSS rule kicks in
    }
  });
})();
ul:empty {
  display: none;
}

ul {
  display: flex;
  flex-wrap: wrap;
  list-style: none;
  margin: 0;
  padding: 0;
}

li {
  flex-basis: calc(33.333% - 10px);
  margin: 5px;
  height: 30px;
  background: lightgray;
}


/* for wider screens, such as desktop */

@media screen and (min-width: 768px) {
  li {
    width: calc(50% - 10px);
  }
}
<ul>
  <li></li>
  <li></li>
</ul>
<ul>
  <li></li>
</ul>
<ul>
  <li></li>
  <li></li>
  <li></li>
</ul>

Markup sample 2

(function() {
  window.addEventListener('load', function() {
    var uls = document.querySelectorAll('ul');
    for (var i = 1; i < uls.length; i++) {
      var lis = uls[i].querySelectorAll('li');
      for (var j = 0; j < lis.length; j++) {
        uls[0].appendChild(lis[j]);        
      }
      uls[i].innerHTML = ''; // make them empty so CSS rule kicks in
    }
  });
})();
ul:empty {
  display: none;
}

ul {
  display: flex;
  flex-wrap: wrap;
  list-style: none;
  margin: 0;
  padding: 0;
}

li {
  flex-basis: calc(33.333% - 10px);
  margin: 5px;
  height: 30px;
  background: lightgray;
}


/* for wider screens, such as desktop */

@media screen and (min-width: 768px) {
  li {
    width: calc(50% - 10px);
  }
}
<ul>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
</ul>
<ul>
</ul>
<ul>
  <li></li>
</ul>

相关问答

更多
  • 在当前的CSS规范中,这是不可能的。 “包含某些特定内容的元素”没有选择器。 它可能在CSS4中,但截至目前,你将不得不采取特殊措施来挑出“特殊”李。
    • Element
    • Another Element
    • Special Element
      • Child
      • Child
  • 得到一个数组,其中包含li和该li的位置索引。 .map()可以和.attr()一起使用。 var arr = $('#ul_li_SubCategories li').map(function (elem) { return { value : $(this).attr('value'), index : $(this).index() } }).get(); $(function() { var arr = $('#ul_li_SubCategor ...
  • 你需要更新你的LESS(请注意我已经更新了字体和内容,所以我没有导入字形字体): ul { padding:20px 0 0 0; & > li { list-style-type: none; & > a:not(:last-child):after { margin-top:3px; content:">"; font-family:'arial ...
  • 首先......你的桌面结构和你的ul-li结构是不同的: 表格单元格都在同一级别// li级别不同 链接名称不同 我想你想做的是: 将“images / user_manager.jpg”图像设置为list-style-image 因为我们没有看到图像而且不知道它们看起来像什么我相信你想要将“images / bg.jpg”图像设置为li-tags的background-image 在这种情况下,截图或链接会很好。 first of all... your table structure and your ...
  • 简单的事实是你使用#worlds-dropdown li而不是.worlds-dropdown li 。 我也不知道为什么你需要allOptions.toggle()因为它隐藏了li东西。 但我现在已经评论过了。 如果这是你的功能,你可以取消注释它。 $("#worldBtn").on("click", function() { $('#displayWorlds').toggle(); }); var allOptions = $(".worlds-dropdown li"); $( ...
  • 你不能使用Flexbox这样做,所以如果你不能使用脚本而你无法改变标记,你可以用float完成这个 ul { list-style: none; margin: 0; padding: 0; } li { float: left; width: calc(33.333% - 10px); margin: 5px; height: 30px; background: lightgray; } /* for wider screens, such ...
  • 数组没有.each函数。 optionTexts.each 你可以做到 $.each(optionTexts, function() { $("#hello").append(this); } ); 在数组上使用jQuery的.each函数 There is no .each function for an array. optionTexts.each You could do $.each(optionTexts, function() { $("#hello").appen ...
  • 相关文章

    更多

    最新问答

    更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(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?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在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)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)