tags:

views:

120

answers:

4

is that even possible?

i have an interface method boolean right(), if it isnt "answering" in a second, it should return false.

+1  A: 

Not possible. This is implementation-specific behaviour.

Sylar
Since you commented on other answers that try to propose solution, I'd just like to point out that while ultimately correct, this answer isn't all that useful. You can't define _ANY_ actual behavior at all in the `interface`. You can't even guarantee that it will _EVER_ terminate. Even if it terminates, you can't guarantee that it will give you what you want. Naturally you can't ensure the behavior that OP asked. Maybe with a richer specification-based static checker, you can try to ensure that an implementation follows a certain behavior, but that's not part of standard Java.
polygenelubricants
+3  A: 

Well, you can't put that requirement in the interface itself, but if you're controlling the calling side it's possible:

  • Spawn a new thread, in which you execute the method-call.
  • In the original thread, join with the new thread with a time out on 1000 milliseconds.
  • If the join times out, kill the thread (or disregard it's result), and return false, otherwise return the result of the call.
aioobe
+3  A: 

Yes, this is possible with e.g. java.util.concurrent.Future<V> (the standard interface to represents the result of an asynchronous computation of type V), combined with the method get(long timeout, TimeUnit unit). The method may throw TimeoutException, among other exceptions, but otherwise returns the computed result on normal execution.

In this case you want a Future<Boolean>, with get(1, TimeUnit.SECONDS) within a try-catch block, handling TimeOutException per your specification.

Available concrete implementations are FutureTask<V> and SwingWorker<T,V>. If this is within the context of Swing application, then you'd want to use the latter.

See also

polygenelubricants
Actually this is not an answer to the question. He asked if the interface itself can provide this behaviour wich is not possible. But your suggestion is a posible solution ofc.
Sylar
while it may not be an answer to my question, it helps alot, ty and also sylar ty for the honest answer ^^
Jan
+3  A: 

Ofcourse it is not possible to add this as a static check (at compile time, something that the compiler can check to ensure it), because how long it takes to run an operation heavily depends on the runtime environment that the program is running in.

You can use the classes in java.util.concurrent to run an operation and wait for it to finish within a certain timeout. A simple example (not thoroughly tested, but demonstrates the idea):

ExecutorService exec = Executors.newSingleThreadExecutor();
Future<Integer> future = exec.submit(new Callable<Integer>() {

    @Override
    public Integer call() throws Exception {
        Thread.sleep(10000);
        return 123;
    }
});

try {
    // Run the callable, wait for max. 5 seconds
    System.out.println(future.get(5, TimeUnit.SECONDS));
}
catch (TimeoutException ex) {
    System.out.println("The operation took too long!");
}
Jesper
+1 for `java.util.concurrent` and code sample.
polygenelubricants
Not a real answer to the question since he asked if the interace itself can provide this behaviour. But is is a possible solution for the problem though.
Sylar
@Sylar In the first sentence I did answer the question: the answer is no.
Jesper
thank you very much too
Jan