2014-11-21 2 views
0

Ниже приведен метод из java.util.concurrent.ScheduledExecutorService:Нужна помощь Понимание этой общей декларации

/** 
* Creates and executes a ScheduledFuture that becomes enabled after the 
* given delay. 
* 
* @param callable the function to execute 
* @param delay the time from now to delay execution 
* @param unit the time unit of the delay parameter 
* @return a ScheduledFuture that can be used to extract result or cancel 
* @throws RejectedExecutionException if the task cannot be 
*   scheduled for execution 
* @throws NullPointerException if callable is null 
*/ 
public <V> ScheduledFuture<V> schedule(Callable<V> callable, 
             long delay, TimeUnit unit); 

Почему <V> ScheduledFuture<V>?

Это первый взгляд, похожий на два типа возврата на метод. Итак, если V, скажем, Boolean, и мы поставляем Callable<Boolean> в качестве первого параметра, каков тип возвращаемого метода? Это Boolean, ScheduledFuture<Boolean> или что-то еще?

Пожалуйста, кто-то, распакуйте это для меня.

+0

Это '' ScheduledFuture . Первый '' просто называет новую общую переменную. –

ответ

3

Почему существует <V> ScheduledFuture<V>?

Потому что это параметр типа и тип возврата.

Часть <V> не является типом возврата, а просто говорит: «Это общий метод с одним параметром типа, V».

Итак, мы имеем:

public           // Access modifier 
<V>            // Type parameter 
ScheduledFuture<V>        // Return type 
schedule           // Method name 
(Callable<V> callable, long delay, TimeUnit unit) // Parameters 
Смежные вопросы