title: How to serialize a lambda?
body: <p>How can I elegantly serialize a lambda?</p>
  
  <p>For example, the code below throws a <code>NotSerializableException</code>. How can I fix it without creating a <code>SerializableRunnable</code> "dummy" interface?</p>
  
  <pre><code>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 = () -&gt; 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();
      }
  }
  </code></pre>