java中的监听器如何工作?

Mic*_*213 5 java javafx

当我们Parent在JavaFx中添加一个侦听器(例如a TextField)时,我们真正在做什么呢?我们是否正在创建一个观察特定内容的线程,TextField当某些内容发生变化时,线程的操作会发生?

我感到很困惑,因为每个线程程序都是按顺序工作的,所以每当它们发生变化时都可以看到一些变量 - 我认为某些变量必须同时执行(?).

Jam*_*s_D 6

您的假设不正确:在向属性添加侦听器时不涉及线程.

JavaFX属性基本上是Observer模式的实现.使用的实现非常复杂,因为这些属性支持"延迟评估",这意味着可以通知其他对象当前值不再有效,但不会重新计算该值,除非其请求.

然而,基本思想非常简单:属性只保留一个监听器列表,并在set调用该方法时通知它们.以下代码不是如何StringProperty在库中实现的,但它会让您了解发生了什么:

public class ExampleStringProperty {

    private final List<ChangeListener<? super String>> changeListeners
        = new ArrayList<>();

    private String value ;

    public String get() {
        return value ;
    }

    public void set(String value) {
        if (! Objects.equals(value, this.value)) {
            String oldValue = this.value ;
            this.value = value ;
            for (ChangeListener<? super String> listener : changeListeners) {
                listener.changed(this, oldValue, value);
            }
        }
    }

    public void addListener(ChangeListener<? super String> listener) {
        changeListeners.add(listener);
    }

    public void removeListener(ChangeListener<? super String> listener) {
        changeListeners.remove(listener);
    }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,不涉及线程:如果设置了属性,则changed(...)在同一线程上调用侦听器的方法.

这是一个快速测试,使用SimpleStringProperty库中的实际:

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class StringPropertyTest {

    public static void main(String[] args) {
        StringProperty prop = new SimpleStringProperty();
        prop.addListener((obs, oldValue, newValue) -> {
            System.out.printf("Property changed from %s to %s on thread %s%n", 
                    oldValue, newValue, Thread.currentThread());
        });

        System.out.println("Running on thread "+Thread.currentThread());
        System.out.println("Setting property to \"value\"");
        prop.set("value");

        System.out.println("Setting property to \"new value\" on thread "+Thread.currentThread());
        prop.set("new value");
    }
}
Run Code Online (Sandbox Code Playgroud)

产生输出

Running on thread Thread[main,5,main]
Setting property to "value"
Property changed from null to value on thread Thread[main,5,main]
Setting property to "new value" on thread Thread[main,5,main]
Property changed from value to new value on thread Thread[main,5,main]
Run Code Online (Sandbox Code Playgroud)