Цитата(Atum @ 29.8.2013, 17:47 ) | | можно пример? |
| Код | private static final int attempts = 10; private static final Timer timer = new Timer(); private static final Random random = new Random();
private static void exec() throws Exception { timer.schedule(new MyTask(attempts), 0L); }
private static boolean foo() { return random.nextBoolean() && random.nextBoolean() && random.nextBoolean(); }
private static class MyTask extends TimerTask { private final int runCount;
private MyTask(int runCount) { this.runCount = runCount; }
@Override public void run() { boolean success = foo(); if (!success) { logger.info("Attempt {} failed", attempts - runCount); if (runCount > 1) { TimerTask newTask = new MyTask(runCount - 1); timer.schedule(newTask, 1000L); } else { logger.error("Execution failed after " + attempts + " attempts"); System.exit(0); } } else { logger.info("Attempt {} success", attempts - runCount); System.exit(0); } } }
|
|