java - 终止属性监听器中的线程 (JavaFX 8)

标签 java multithreading javafx javafx-8 preemptive

我知道 Java 的实际模型是针对协作线程的,并且它不会强制线程终止。

由于 Thread.stop() 已被弃用(出于上面列出的原因)。我试图通过 BooleanProperty 监听器停止线程。

这是 MCVE:

TestStopMethod.java

package javatest;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ObservableValue;
public class TestStopMethod extends Thread {
    private BooleanProperty amIdead = new SimpleBooleanProperty(false);
    public void setDeath() {
        this.amIdead.set(true);
    }

    @Override
    public void run() {
        amIdead.addListener((ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) -> {
            System.out.println("I'm dead!!!");
            throw new ThreadDeath();
        });
        for(;;);
    }
}

WatchDog.java

package javatest;

import java.util.TimerTask;

public class Watchdog extends TimerTask {
    TestStopMethod watched;
    public Watchdog(TestStopMethod target) {
        watched = target;
    }
    @Override
    public void run() {
        watched.setDeath();
        //watched.stop(); <- Works but this is exactly what I am trying to avoid
        System.out.println("You're dead!");
    }

}

驱动程序.java

package javatest;

import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Driver {

    public static void main(String[] args) {
        try {
            TestStopMethod mythread = new TestStopMethod();
            Timer t = new Timer();
            Watchdog w = new Watchdog(mythread);
            t.schedule(w, 1000);
            mythread.start();
            mythread.join();
            t.cancel();
            System.out.println("End of story");
        } catch (InterruptedException ex) {
            Logger.getLogger(Driver.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

最佳答案

如果您更改属性值,则会在更改属性的同一线程上调用监听器(只需考虑一下您将/可以如何实现属性类)。所以在你的例子中 ThreadDeath支持 Timer 的线程抛出错误例如,这并不是您真正想要的。

从外部(对该线程)终止线程的正确方法是设置一个标志,然后在线程的实现中定期轮询该标志。这实际上比听起来更棘手,因为必须从多个线程访问该标志,因此必须正确同步对它的访问。

幸运的是,有一些实用程序类可以帮助解决这个问题。 FutureTask ,例如,包装 RunnableCallable并提供cancel()isCancelled()方法。如果您使用 JavaFX,那么 javafx.concurrent API 提供 Callable 的一些实现和Runnable这样做,并且还提供专门用于在 FX 应用程序线程上执行代码的功能。看看documentation for javafx.concurrent.Task举一些例子。

例如,您可以这样做:

package javatest;
public class TestStopMethod implements Runnable {

    @Override
    public void run() {
        try {
            synchronized(this) {
                for(;;) {
                    wait(1); 
                }
            }
        } catch (InterruptedException exc) {
            System.out.println("Interrupted");
        }
    }
}

看门狗.java:

package javatest;

import java.util.TimerTask;
import java.util.concurrent.Future;

public class Watchdog extends TimerTask {
    Future<Void> watched;
    public Watchdog(Future<Void> target) {
        watched = target;
    }
    @Override
    public void run() {
        watched.cancel(true);
        //watched.stop(); <- Works but this is exactly what I am trying to avoid
        System.out.println("You're dead!");
    }
}

驱动程序.java:

package javatest;

import java.util.*;
import java.util.concurrent.FutureTask;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Driver {

    public static void main(String[] args) {
        try {
            FutureTask<Void> myTask = new FutureTask<>(new TestStopMethod(), null);
            Timer t = new Timer();
            Watchdog w = new Watchdog(myTask);
            t.schedule(w, 1000);
            Thread mythread = new Thread(myTask);
            mythread.start();
            mythread.join();
            t.cancel();
            System.out.println("End of story");
        } catch (InterruptedException ex) {
            Logger.getLogger(Driver.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

在 JavaFX 应用程序中,您可以这样做。请注意,如果您尝试在没有运行 FX 应用程序线程的情况下执行此操作,事情会变得很糟糕,因为 cancelled FX 中的标志 Task必须在该线程上更新。

package javatest;

import javafx.concurrent.Task;

public class TestStopMethod extends Task<Void> {

    @Override
    public Void call() {
        System.out.println("Calling");
        while (true) {
            if (isCancelled()) {
                System.out.println("Cancelled");
                break ;
            }
        }
        System.out.println("Exiting");
        return null ;
    }
}

看门狗.java:

package javatest;

import java.util.TimerTask;

import javafx.concurrent.Task;

public class Watchdog extends TimerTask {
    Task<Void> watched;
    public Watchdog(Task<Void> target) {
        watched = target;
    }
    @Override
    public void run() {
        watched.cancel();
        //watched.stop(); <- Works but this is exactly what I am trying to avoid
        System.out.println("You're dead!");
    }

}

驱动程序.java

package javatest;

import java.util.Timer;
import java.util.logging.Level;
import java.util.logging.Logger;

import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Driver extends Application {

    @Override
    public void start(Stage primaryStage) {
        try {

            TextArea console = new TextArea();
            BorderPane root = new BorderPane(console);
            Scene scene = new Scene(root, 600, 400);
            primaryStage.setScene(scene);
            primaryStage.show();

            Task<Void> myTask = new TestStopMethod();
            Timer t = new Timer();
            Watchdog w = new Watchdog(myTask);
            t.schedule(w, 1000);
            Thread mythread = new Thread(myTask);
            mythread.setDaemon(true);

            myTask.stateProperty().addListener((obs, oldState, newState) -> {
                console.appendText("State change "+oldState+" -> "+newState+"\n");
                if (oldState == Worker.State.RUNNING) {
                    t.cancel();
                    console.appendText("End of Story\n");
                }
            });
            mythread.start();

        } catch (Exception ex) {
            Logger.getLogger(Driver.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

关于java - 终止属性监听器中的线程 (JavaFX 8),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26263152/

相关文章:

java - 给出了替代密码加密器代码,但没有解密器

python - 如何将两个参数传递给 Pool.starmap()?

java - Java中用于处理任务的并发和可扩展数据结构?

java - 单击时的actionEvent,希望与输入键java fx相同

java - 按值获取字段

java - 无法在消费者线程中使用 JNI 的生产者消费者程序中捕获 SIGINT 信号

java - java的image magick问题

c# - 如何在C#中使用多线程下载单个文件

JavaFX/FXML 将 ChoiceBox 添加到根 Pane

java - javafx 中代码区域的自动补全