如何在 Java 中与两(2)个 SwingWorker 类共享数据
我有两个 SwingWorker 类:FileLineCounterThread 和 FileDivisionThread
我将执行这两个线程。当行计数线程完成时,它将结果传递给文件分割线程。
我不知道如何将结果传递给启动的线程。
I have two SwingWorker class: FileLineCounterThread
and FileDivisionThread
I will execute the two threads. When the lines counting thread finishes, it will pass the result to File Division thread.
I do not have an idea on how to pass the result to started thread.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
SwingWorker.execute()
是 buggy 并且会只串行执行任务。使用 ExecutorService.execute() 实现并发:SwingWorker.execute()
is buggy and will only execute tasks serially. UseExecutorService.execute()
for concurrency:用于字符数据和字符数据的 PipedReader/Writer 二进制数据的 PipedInput/OutputStream。
java.io 中
问候,
史蒂芬
PipedReader/Writer for character data & PipedInput/OutputStream for binary data
in java.io.
Regards,
Stéphane
永远不要放弃,永远不要放弃 Executor 和 SwingWorker
1/ Executor 和 SwingWorker
2/ 保存并检查由 Executor 和实时 SwingWorkers 线程,旨在避免捕获上述错误
3/检查最大数量对于执行者或限制为最终成员
编辑按OP的要求更改
never hands up, never surrender its possible with Executor and SwingWorker
1/ bug for Executor and SwingWorker
2/ hold and check number of thread started by Executor and live SwingWorkers threads with intentions to avoid caught above mentioned bug
3/ check maximum numbers for Executor or restict that to final munber
EDIT changed by OP's requirements
我不确定这是您应该使用的解决方案,并且它破坏了您使用 SwingWorker 获得的简单性和安全性,但为了完整起见,我将提及它。
将两个字段放在两个线程都可以看到的位置:一个布尔值,称为
hasValue
,初始化为 false,以及一个 int(或 long),称为countValue
。两者都必须声明为易失性
。当计数器线程完成后,将计数放入countValue
中。 然后将hasValue
设置为true。然后,除法线程可以定期检查“hasValue”并在可用时获取计数。如果该除法在获得计数后提供的值会更准确,那么就可以了。更有可能的是,它正在做一些工作,然后等待计数。在本例中,设置名为
countMonitor
的第三个字段,定义为final Object
。当它完成初始工作时,让它检查hasValue
。如果是 true,则获取该值并继续。如果为 false,则调用countMonitor
上的wait
方法,并在收到通知时继续。计数器线程完成后,应始终在将值放入hasValue
和 <代码>计数值。我在这里遗漏了一点。
Object
的 javadoc 将告诉您所需的同步和检查的异常。您的设计足够简单,您不会被多线程生成的常见超自然恐怖故事所困扰。我希望。但如果您走这条路,您可能需要做一些研究。 (如果您在同一会话中重复整个过程,您肯定想要进行大量研究。)I am not sure this is a solution you should use, and it undermines the simplicity and safety you get from using SwingWorker, but I'll mention it for completeness.
Put two fields where both threads can see them: one boolean, called
hasValue
, initialized to false, and one int (or long) calledcountValue
. Both must be declared asvolatile
. When the counter thread is done, put the count incountValue
. Then sethasValue
to true. The division thread can then check `hasValue' periodically and grab the count when it is available.If the division is providing values that will be more accurate once it gets the count, this will do. More likely, it is doing some work, then waiting for the count. In this case, set up a third field called
countMonitor
, defined asfinal Object
. When it finishes the initial work, have it checkhasValue
. If it's true, grab the value and continue. If it's false, call thewait
method oncountMonitor
and continue when notified. The counter thread, when done, should always call thenotifyAll
method oncountMonitor
after putting values inhasValue
andcountValue
.I've left out a bit here. The javadoc for
Object
will tell you about needed synchronization and checked exceptions. Your design is straightforward enough that you won't be troubled with the usual supernatural horror stories multi-threading generates. I hope. But you might want to do a bit of research if you go this route. (If you repeat the whole process in the same session, you will definitely want to do a lot of research.)