当我取消 SwingWorker 时,Java SwingWorker Socket Server 不会被取消
我有一个作为 Java SwingWorker 运行的 Socket 服务器。
SwingWorker 接收传入连接,并在连接后将套接字移交给负责连接的执行器。 Executor 调用 SocketHandler,该 SocketHandler 将通过 run() 方法处理套接字。
现在,我的 GUI 上有一个按钮表示停止。我想在按下“停止”按钮时停止 SwingWorker 的执行。为此,我在按下“停止”按钮时调用了一个 cancel(true) 方法。
现在,尽管我的程序调用了 cancel(true) 方法,SocketHandler 仍继续接收传入数据。这是怎么回事?
我读过几篇文章,这是 JDK 中的一个错误,调用 cancel(true) 方法不会停止 SwingWorker。这是真的吗?
I got a Socket Server running as a Java SwingWorker.
The SwingWorker receives incoming connections, and upon connection, hands over the socket to an Executor that takes care of the connection. The Executor calls a SocketHandler that will handle the socket via the run() method.
Now, I have one button on the GUI that says stop. I want to stop the SwingWorker from execution when i press the Stop button. To do so, I had a cancel(true) method called upon pressing the Stop button.
Now, the SocketHandler continues receiving incoming data despite my program calling the cancel(true) method. What's going on here?
I had read thru couple articles that it's a bug within the JDK that calling cancel(true) method does not stop the SwingWorker. Is this true?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需在循环中添加一个布尔检查器
if (!stillWorking)return;
然后您可以将布尔值设置为 false 以停止该方法。
simply add a boolean checker in the loop
if (!stillWorking)return;
then you can set the boolean to false to stop the method.
public final boolean cancel(boolean mayInterruptIfRunning)
的 API:“
mayInterruptIfRunning
-true
如果执行此任务的线程应被中断;否则,在-允许完成进度任务”。您必须阅读
Thread.interrupt
。它实际上不会打断任何事情;您必须在代码中指定如何处理中断。另外,从您的问题中不清楚您是否只是想阻止主
SwingWorker
获取连接,或者是否也想终止所有现有连接。编辑:如果你在接受连接的线程中有一个
while
循环:我想应该足够了。
现在,当
SwingWorker
被取消时,循环应该停止,因为SwingWorker
线程设置了内部中断标志。API for
public final boolean cancel(boolean mayInterruptIfRunning)
:"
mayInterruptIfRunning
-true
if the thread executing this task should be interrupted; otherwise, in-progress tasks are allowed to complete".You have to read on
Thread.interrupt
. It actually does not interrupt anything; you have to specify in your code how to deal with interrupts.Also, it's not clear from your question if you just want to stop the main
SwingWorker
from taking connections or if you want to kill all existing connections too.EDIT: If you do have a
while
loop in the thread accepting the connections:should be sufficient, I guess.
Now when the
SwingWorker
is cancelled, the loop should stop because theSwingWorker
thread has it inner interrupt flag set.@Yakult121,这不是您两个问题的答案 当我取消 SwingWorker 和 Java SwingWorker - 使用发布/进程更新 TextArea EDT?,我认为你的问题没有完全描述,我错过了很多重要的细节,
你必须发布代码才能更好......,这演示了你对 Executor 的实现, SwingWorker 和取消
您的代码也可以基于这两个线程
1/ 取消 SwingWorker
2/ 执行器和 SwingWorker
3/ 和如何/如何确定具体的 SwingWorker 取消线程
4/真正将两个线程放在一起
@Yakult121 please this isn't answer to your both questions Java SwingWorker Socket Server does not get cancelled when i cancel the the SwingWorker and Java SwingWorker - using publish/process to update a TextArea in EDT? , I think that your question not fully described, I miss there lots of importan details,
you have to post code for better ..., that demostrated your implementations for Executor, SwingWorker and cancelation for that
your code could be based on this two threads, too
1/ Cancel SwingWorker
2/ Executor And SwingWorker
3/ and how/what you determine concrete SwingWorker's thread for cancelations
4/ really put your two thread together