2014-11-21 38 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