Bagaimana saya bisa membuat serial lambda secara elegan?
Misalnya, kode di bawah ini melempar a NotSerializableException
. Bagaimana saya bisa memperbaikinya tanpa membuat SerializableRunnable
antarmuka "dummy"?
public static void main(String[] args) throws Exception {
File file = Files.createTempFile("lambda", "ser").toFile();
try (ObjectOutput oo = new ObjectOutputStream(new FileOutputStream(file))) {
Runnable r = () -> System.out.println("Can I be serialized?");
oo.writeObject(r);
}
try (ObjectInput oi = new ObjectInputStream(new FileInputStream(file))) {
Runnable r = (Runnable) oi.readObject();
r.run();
}
}