JavaFX 多线程

发布于 2024-09-06 22:05:33 字数 719 浏览 0 评论 0原文

我正在编写一个小程序,其中 JavaFx 充当查看器和控制器,并让 Java 完成其他艰苦的工作。我可以从 Javafx 启动多个线程,但是我无法停止它们。如果我尝试使用 .stop(),线程仍在运行。

这是其中之一:

public var sleepTask_connect;

function LogOutAction(): Void {
    sleepTask_connect.stop();
}

function LogInAction(): Void {

   var listener = FXListener_interface_connection {
                override function callback(errorCode, errorMessage): Void {
                    //do something
                    if(errorCode != 200){
                        setIcn(errorMessage);
                        }
                }
            }
    sleepTask_connect = FXListener_connection {
                listener: listener
            };
    sleepTask_connect.start();

}

I'm writing a small programm where JavaFx acts as a viewer and controler and let Java do the other hard work. I can start multiple threads from Javafx however, I'm not able to stop them. If I try to use .stop(), the threads are still running.

Here is one of them:

public var sleepTask_connect;

function LogOutAction(): Void {
    sleepTask_connect.stop();
}

function LogInAction(): Void {

   var listener = FXListener_interface_connection {
                override function callback(errorCode, errorMessage): Void {
                    //do something
                    if(errorCode != 200){
                        setIcn(errorMessage);
                        }
                }
            }
    sleepTask_connect = FXListener_connection {
                listener: listener
            };
    sleepTask_connect.start();

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忱杏 2024-09-13 22:05:33

使用 JavaTaskBase 来实现你的Java线程。有一个 stop 方法可以终止线程。这是一个如何使用它的示例

Use JavaTaskBase to implement you Java thread. There is a stop method to kill the thread. Here is an example of how you use it.

我不会写诗 2024-09-13 22:05:33

我在 JFXtras XWorker 线程组件方面运气更好。请参阅 http ://jfxtras.googlecode.com/svn/site/javadoc/release-0.6/org.jfxtras.async/org.jfxtras.async.XWorker.html

然而,一般来说,为了让您的线程响应取消/停止请求,您必须在“执行某些操作”部分检查代码中的取消或停止标志。例如,如果您的线程处于无限循环中,或者如果您只有一系列长时间运行的进程,则可以检查它们之间是否已取消/停止。或者,如果您的代码调用某些阻塞方法(如套接字或阻塞队列),那么当线程取消时,大多数这些方法都会抛出 InterruptedException。

I've had better luck with the JFXtras XWorker component for threading. See http://jfxtras.googlecode.com/svn/site/javadoc/release-0.6/org.jfxtras.async/org.jfxtras.async.XWorker.html.

However in general in order for your thread to respond to cancel/stop requests, you have to check the canceled or stopped flag in your code during your "do something" section. This works if your thread is in an infinite loop for example, or if you just have a series of long running processes you can check for canceled/stopped in between them. Alternatively, if your code calls some blocking method (like sockets or a blocking queue), then most of these will throw an InterruptedException when the thread is canceled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文