Cancelling method calls when the same method is called multiple time

后端 未结 3 734
梦如初夏
梦如初夏 2021-01-13 06:05

I think there\'s probably a name for what I\'m describing here, but I don\'t know it. So my first question would be to know the name of this technique.

Here\'s an ex

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-13 06:27

    This is not solvable in Java without using some extra infrastructure like you did with executor and futures. It is not possible to solve this in syntactically concise manner in Java.

    You will always need some sort of method result wrapper, because the mechanism returns immediately but the actual result is retrieved later. In your case this was accomplished via Future.

    You will always need to be able to specify code to be executed in a manner that will allow delayed execution. In most languages this is accomplished using function pointers or function values or closures. In Java, lacking these language features, this is usually accomplished by passing an object that implements some sort of interface such as Runnable, Callable, that allows delayed execution of a block of code. There are other options but none of them are simple, such as using a dynamic proxy.

    tl;dr

    Can't do this in concise manner in Java.

提交回复
热议问题