在容器中等待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 aawait()
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 theCountDownLatch
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
最满意答案
您可以使用
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 ofEntry
.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_ ...
-
python 3.4 tkinter框架没有填充垂直方向(python 3.4 tkinter frame does not fill in vertical direction)[2022-04-13]
您需要传递值大于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行,则所有前一行的行号不受影响。 如果您只想撤消一系列操作,可以 ...