18

In Java 9 does Flow API replace Observer and Observable? If not, what does?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Denis Murphy
  • 1,137
  • 1
  • 11
  • 21

1 Answers1

19

The new Flow API is designed as a common denominator for reactive stream libraries like RxJava and Reactive X. Building on Java 9, they can have their types extend the new interfaces (or so the thought goes). While it is of course charming to use the API inside the JDK, that is not the case in Java 9 and there are no concrete plans to introduce it (to the best of my knowledge).

Regarding Observer and Observable the issue which triggered the deprecation states:

Application developers should consider using java.beans for a richer change notification model. Or they should consider constructs in java.util.concurrent such as queues or semaphores to pass messages among threads, with reliable ordering and synchronization properties.

These are recommendations for application developers for writing new code. It gives no advice on updating existing code or what to do inside the JDK. I guess the reason for that is that both cases are supposed to stay as they are.

Note that Java does not use @Deprecated to necessarily mean "will be removed". Instead it can also mean "use better alternatives" and I think that is the case here. So to answer your question in a few words:

In Java 9 does Flow API replace Observer and Observable

No.

and if it doesn't what does.

Nothing.

Nicolai Parlog
  • 47,972
  • 24
  • 125
  • 255
  • 4
    +1. Note that since [JEP 277](http://openjdk.java.net/jeps/277) an API could also be earmarked for removal in a future release using the `@Deprecated` annotation. I'm not aware that this new possibility has been used up to now. It could in the future, but I doubt that they would ever deprecate Observer/Observable "forRemoval". – Stefan Zobel Mar 01 '17 at 14:40
  • 5
    Java 9 actually removes some methods that were deprecated in Java 8. I have no idea whether that is the first time but it is surely not common. I never meant to say that `@Deprecated` _never_ means that something might be removed but my phrasing was ambiguous. Thanks to your comment I realized that and put in a _necessarily_. – Nicolai Parlog Mar 01 '17 at 20:16
  • 1
    That's interesting. Can you share an example? – Stefan Zobel Mar 01 '17 at 20:19
  • 7
    Sure. :) Six methods adding/removing `PropertyChangeListener` got removed from [`LogManager`](https://docs.oracle.com/javase/8/docs/api/java/util/logging/LogManager.html), [`Pack200.Packer`](https://docs.oracle.com/javase/8/docs/api/java/util/jar/Pack200.Packer.html), and [`Pack200.Unpacker`](https://docs.oracle.com/javase/8/docs/api/java/util/jar/Pack200.Unpacker.html) (search for _Deprecated_). They were removed because they would have created cycles between modules. – Nicolai Parlog Mar 01 '17 at 22:04
  • And I think this is indeed the first time anything has actually been removed, so this is pretty news worthy. – Jason Mar 20 '17 at 11:01