首页 \ 问答 \ 在容器中等待CountDownLatch的多个实例(Awaiting multiple instances of CountDownLatch in container)

在容器中等待CountDownLatch的多个实例(Awaiting multiple instances of CountDownLatch in container)

我写了一个类CountDownLatchContainer它有一个await()方法,它等待所有CountDownLatches。 一切工作正常。

public final class CountDownLatchContainer
{
  private final Set<CountDownLatch> countDowns;

  public CountDownLatchContainer(List<CountDownLatch> countDowns)
  {
    this.countDowns = new HashSet<CountDownLatch>(countDowns);
  }

  public void await() throws InterruptedException
  {
    for (CountDownLatch count : countDowns)
      count.await();
  }
}

为了科学和艺术(D),我想扩展功能,并从CountDownLatch类中添加public boolean await(long timeout, TimeUnit unit)

我希望每个倒数锁存器都具有相同的超时时间,并且总的方法只是为了阻塞timeout TimeUnits的数量。 实现这一点很困难。 我拥有的是:

  public boolean await(long timeout, TimeUnit unit) throws InterruptedException
  {
    boolean success = true;
    for (CountDownLatch count : countDowns)
      success &= count.await(timeout / countDowns.size(), unit);
    return success;
  }

所以,总体超时是照顾的,但每一次倒计时只占整个时间的百分比。

那么我怎样才能给单个锁存器提供与方法参数中指定的时间相同的时间量,而不会超过总时间?

在这里可视化。 Thx为了ascii艺术灵感的六合一。

|-----------------------------|
             Total
|-----------------------------|
  L1       
|-----------------------------|
  L2       
|-----------------------------|

I wrote a class CountDownLatchContainer it has a await() method, that waits on all CountDownLatches. Everything is working fine.

public final class CountDownLatchContainer
{
  private final Set<CountDownLatch> countDowns;

  public CountDownLatchContainer(List<CountDownLatch> countDowns)
  {
    this.countDowns = new HashSet<CountDownLatch>(countDowns);
  }

  public void await() throws InterruptedException
  {
    for (CountDownLatch count : countDowns)
      count.await();
  }
}

For the sake of science and art (:D), I wanted to extend the functionality and add the public boolean await(long timeout, TimeUnit unit) from the CountDownLatch class.

I want each countdown latch to have the same timeout and the method overall just to block for the number of timeout TimeUnits. I have difficulties achieving that. What I have is:

  public boolean await(long timeout, TimeUnit unit) throws InterruptedException
  {
    boolean success = true;
    for (CountDownLatch count : countDowns)
      success &= count.await(timeout / countDowns.size(), unit);
    return success;
  }

So, the overall time out is taken care of, but each count down has just a percentage of the overall time.

So how can I give the single latches the same amount of time as specified in the method parameter without execeeding the total time?

Here a visualization. Thx to hexafraction for the ascii art inspiration.

|-----------------------------|
             Total
|-----------------------------|
  L1       
|-----------------------------|
  L2       
|-----------------------------|

原文:https://stackoverflow.com/questions/18663436
更新时间:2023-08-01 20:08

最满意答案

您可以使用Entry xview方法。 xview返回条目中显示的文本的可见部分。 您可以使用它以交互方式创建适合条目的文本。

这是一个快速而肮脏的概念证明

from tkinter import *

root = Tk()

v = StringVar(root)
e = Entry(root,textvariable=v)
e.pack(fill=BOTH)
v.set('abcdefghijklmnopqrstuvwxyz0123456789')

new_s = None

def check_length():
    global new_s
    original_s = v.get()

    def shorten():
        global new_s
        e.xview(0)
        if e.xview()[1] != 1.0:
            new_s = new_s[:-4] + '...'
            v.set(new_s)
            print("new_s: " + new_s)
            e.xview(0)
            e.after(0,shorten)
            print(e.xview()[1])
    if e.xview() != (0.0,1.0):
        new_s = original_s + '...'
        shorten()

b = Button(root,text="hop",command=check_length)
b.pack()

e.mainloop()

You can use xview method of Entry. xview return the visible part of the text displayed in the entry. You can use it to interactively create a text that fit the entry.

Here is a quick and dirty proof of concept

from tkinter import *

root = Tk()

v = StringVar(root)
e = Entry(root,textvariable=v)
e.pack(fill=BOTH)
v.set('abcdefghijklmnopqrstuvwxyz0123456789')

new_s = None

def check_length():
    global new_s
    original_s = v.get()

    def shorten():
        global new_s
        e.xview(0)
        if e.xview()[1] != 1.0:
            new_s = new_s[:-4] + '...'
            v.set(new_s)
            print("new_s: " + new_s)
            e.xview(0)
            e.after(0,shorten)
            print(e.xview()[1])
    if e.xview() != (0.0,1.0):
        new_s = original_s + '...'
        shorten()

b = Button(root,text="hop",command=check_length)
b.pack()

e.mainloop()

相关问答

更多
  • 你的第一个问题是,即使在你的程序启动之前,调用get entry = E1.get()也会发生,所以很明显, entry会指向一些空字符串。 最终的第二个问题是,无论如何只有在mainloop完成后才能打印文本,即关闭tkinter应用程序。 如果要在程序运行时打印Entry小部件的内容,则需要安排回调。 例如,您可以按如下方式聆听键 import Tkinter as tk def on_change(e): print e.widget.get() root = tk.Tk ...
  • 这是因为使用P.isdigit()您将内容限制为数字。 当您将111替换为9时,有一个步骤,其中条目的内容是空字符串。 由于''.isdigit()为false,因此小部件始终将数字作为其内容。 删除此约束,您允许任何字符串,然后您可以完全替换该值。 That's because with P.isdigit() you are restricting the content to be a digit. When you replace 111 to 9, there is a step in which ...
  • BarVolSyringe1在BarVolSyringe1中未定义,它是sDown的局部变量。 在要共享的函数外部创建条形: BarVolSyringe1 = ttk.Progressbar(mGui, orient='vertical', length=78, mode='determinate', value = 100) BarVolSyringe2 = ttk.Progressbar(mGui, orient='vertical', length=78, mode='determinate', va ...
  • 您可以使用Entry xview方法。 xview返回条目中显示的文本的可见部分。 您可以使用它以交互方式创建适合条目的文本。 这是一个快速而肮脏的概念证明 from tkinter import * root = Tk() v = StringVar(root) e = Entry(root,textvariable=v) e.pack(fill=BOTH) v.set('abcdefghijklmnopqrstuvwxyz0123456789') new_s = None def check_l ...
  • 进行验证的正确方法是使用validatecommand选项,而不是使用trace 。 通过内置到Widget中的验证功能,您不需要对Widget本身的引用(尽管您可以使用它)。 当validatecommand运行时,如果输入有效,可以让它传入新值。 你只需要检查这个值,然后返回True或False ,而不必知道它适用于哪个小部件。 例如: import tkinter as tk def validate_input(new_value): valid = new_value .isdigit( ...
  • 当选择下拉菜单中的其他选项时,您需要删除之前的内容,然后创建新的小部件 在您创建形状的每个函数中,您需要首先销毁小部件,然后创建新小部件 我已修复代码: from tkinter import * global widgets # this list will contain widgets to be deleted widgets = [] def square(): global widgets for widget in widgets[:]: widget.des ...
  • 这是您正确运行的代码的简化版本: #!/usr/bin/env python ''' Toggle disable / normal of Tkinter widgets Written by PM 2Ring & R. Murray 2015.11.15 See http://stackoverflow.com/q/33711472/4014959 ''' #Python 3 / Python 2 Tkinter import try: import tkinter as t ...
  • 我使用fill=Y选项打包self.frameLEFT以便两个列表框位于窗口的顶部(否则self.frameLEFT的高度将适合两个列表框, self.frameLEFT将在窗口中垂直居中)。 对于self.frameRIGHT我使用expand=True和fill=BOTH以便它填充窗口中的所有可用空间。 最后,对于条目e ,我使用fill=X以便它填充可用的水平空间。 from tkinter import * import os class TechnologyIDE: def __init_ ...
  • 您需要传递值大于0的expand选项 ,以告知包几何管理器为窗口小部件分配附加空间。 mainframe.pack(side=TOP, fill=BOTH, expand=1) 否则,窗口小部件将不会展开。 请参阅模式 - 填充整个父窗口小部件 - Tkinter Pack几何管理器 。 You need to pass expand option with value larger than 0 to tell pack geometry manager to assign additinoal spa ...
  • 文本小部件有一个删除方法,它将删除您想要的任何文本范围。 它完全应该做到 - 它给它两个字符位置,它将删除它们之间的文本。 例如,要删除整个第二行文本,您可以执行以下操作: text_entry.delete("2.0", "3.0") 如果您尝试记住文本范围以便以后删除它们,请确保从下往上删除。 如果从上到下删除,则第一次删除将导致所有其他已保存的索引不正确。 如果要删除第2行和第4行,删除第2行时,第4行将变为第3行,等等。如果先删除第4行,则所有前一行的行号不受影响。 如果您只想撤消一系列操作,可以 ...

相关文章

更多

最新问答

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