protected <T> T timeout(Callable<T> waitFor, Callable<Object> run, int sleep, int operationTimeout) {
		try {
			long start = System.currentTimeMillis();
			boolean timeout = false;
			T value = null;
			do {
				value = waitFor.call();
				
				if (run != null) {
					run.call();
				} 
				
				if (sleep > 0) {
					try {
						Thread.sleep(sleep);
					} catch (InterruptedException e) {
						break;
					}
				}
				
				timeout = System.currentTimeMillis() - start > operationTimeout;
			} while (!Thread.interrupted() && value == null && !timeout);
			if (timeout) {
				throw new RuntimeException("Timed out waiting for result: " + waitFor);
			}
			return value;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}