Following a discussion in the Netz39 IRC channel I created a short Java Enterprise Loop for those in need.
package com.penguineering.seriousnonsense; /* * Short usage instructions: * * final Runnable loopee = ... // Acquire some runnable instance * EnterpriseLoop.forRunnable(loopee).run(); * * or put it in a thread: * * new Thread(EnterpriseLoop.forRunnable(loopee)).start(); * * * Whatever kills your design best. */ public class EnterpriseLoop implements Runnable { public static EnterpriseLoop forRunnable(final Runnable _loopee) throws NullPointerException { return new EnterpriseLoop(_loopee); } private final Runnable loopee; private EnterpriseLoop(final Runnable _loopee) throws NullPointerException { if (_loopee == null) throw new NullPointerException("Runnable _loopee must not be null!"); this.loopee = _loopee; } public Runnable getLoopee() { return loopee; } @Override public void run() { while (true) try { loopee.run(); } catch {Throwable t) { // ignore } } }
To adhere to development standards usually found in situations leading to the need of an enterprise loop, I neither documented the code (except for a short usage instruction to save you from thinking about those lines you are going to copy) nor tested it.
I leave this to the public domain to successfully serve as a bad example.