2014-11-21 40 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是,讓我們說布爾,並且我們提供Callable<Boolean>作爲第一個參數,該方法的返回類型是什麼?是布爾,ScheduledFuture<Boolean>還是別的?

請有人給我解開這個。

+0

這是'ScheduledFuture '。第一個''只是命名新的通用變量。 – 2014-11-21 21:34:57

回答

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