+ public Exception runIdle(MainProgramRunnable runnable) {
+ try {
+ long startTime = System.currentTimeMillis();
+ while (true) {
+ boolean hasMutex = false;
+ try {
+ synchronized (MainProgram.this) {
+ if (hasMutex = mutex.tryAcquire()) {
+ if (operations.isEmpty()) {
+ runnable.run();
+ return null;
+ }
+ }
+ }
+ long endTime = System.currentTimeMillis();
+ if ((endTime - startTime) > 100) {
+ startTime = endTime;
+ LOGGER.info("MainProgram.runIdle() retry mutex acquire!");
+ }
+ } catch (Exception e) {
+ return e;
+ } finally {
+ if (hasMutex)
+ mutex.release();
+ }
+ }
+ } finally {
+ runnable.done();
+ }
+ }
+