白季飞龙的个人主页

java-observable

Java Observable 示例

package bj;

import java.util.Observable;
import java.util.stream.IntStream;
import java.util.stream.Stream;

class App {
    public static void main(String[] var0) throws Throwable {
        new Observable() {{
            IntStream.rangeClosed(1, 3).forEach($ -> this.addObserver((o, arg) -> System.out.println("updated received by " + $)));
            Stream.generate(() -> null).limit(3).forEach($ -> {
                        this.clearChanged();
                        this.setChanged();
                        this.notifyObservers();
                    }
            );
        }};
    }
}

输出:

updated received by 3
updated received by 2
updated received by 1
updated received by 3
updated received by 2
updated received by 1
updated received by 3
updated received by 2
updated received by 1

漫漫路,莫论逍遥;潜心修,只为悟道