Search code examples
javafork-joinrecursivetask

Why RecursiveTask implements Serializable in fork join?


I am planning to use Fork/Join in my application by extending the RecursiveTask class. I notice that it is Serializable. I have a few fields of non-serializable types that I want to keep in it. I could make them transient but that would make them null after some serde op and I don't want that.

I want to know Why RecursiveTask class implements Serializable interface as I don't see the need. Also, under what circumstances the instances of class extending it will be serialized?


Solution

  • RecursiveTask inherits Serializable from ForkJoinTask and it is documented here: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinTask.html

    ForkJoinTasks are Serializable, which enables them to be used in extensions such as remote execution frameworks. It is sensible to serialize tasks only before or after, but not during, execution. Serialization is not relied on during execution itself.