修复未来未检查的分配警告
Fix Future Unchecked Assignment Warning
我有一个代码可以 ping 给定子网中的所有 IP 地址。它使用并发来提高性能,因为等待每个 IP 地址的超时将花费更长的时间:
/**
* @param subNetwork The subnet to scan
* @return A list of internet protocol addresses that are reachable
* @throws IOException
* @throws ExecutionException
* @throws InterruptedException
*/
public static List<String> getRespondingInternetProtocolAddresses(final String subNetwork) throws IOException,
ExecutionException,
InterruptedException
{
final List<String> activeInternetProtocolAddresses = new ArrayList<>();
int startingIndex = 1;
int upperBound = 256;
int poolSize = upperBound - 1; // Query concurrently for best time savings
ExecutorService threadPool = Executors.newFixedThreadPool(poolSize);
List<Future<Runnable>> tasks = new ArrayList<>();
for (int currentSubNetIndex = startingIndex; currentSubNetIndex < upperBound;
currentSubNetIndex++)
{
final int subNetIndex = currentSubNetIndex;
// Query each Internet protocol address concurrently for speed purposes
Future task = threadPool.submit(new Thread(() ->
{
String currentInternetProtocolAddress = subNetwork + "." + subNetIndex;
try
{
if (Ping.isReachable(currentInternetProtocolAddress))
{
activeInternetProtocolAddresses.add(currentInternetProtocolAddress);
}
} catch (IOException exception)
{
exception.printStackTrace();
}
}));
tasks.add(task); // TODO Fix unchecked assignment warning
}
for (Future<Runnable> task : tasks)
{
task.get();
}
threadPool.shutdown();
return activeInternetProtocolAddresses;
}
在任务列表中添加新任务时,我收到了未经检查的分配警告:
tasks.add(task);
我尝试将 Future
by replacing it with Future<Runnable>
but it created a compile error instead since submit()
returns 泛化为 Future<?>
。
我该怎么做才能修复警告?
Future<T>
在应从任务返回类型 T
的某些结果时使用 - 然后您将获得此结果并使用它。
如果您只需要等到所有任务执行完毕,请使用 threadPool.submit(Runnable r)
。然后在 threadPool.shutdown()
调用 threadPool.awaitTermination()
之后——阻塞直到所有任务完成。
要解决此问题,您可以将任务声明为 List<Future<?>>
并将 task
声明为 Future<?>
。
我有一个代码可以 ping 给定子网中的所有 IP 地址。它使用并发来提高性能,因为等待每个 IP 地址的超时将花费更长的时间:
/**
* @param subNetwork The subnet to scan
* @return A list of internet protocol addresses that are reachable
* @throws IOException
* @throws ExecutionException
* @throws InterruptedException
*/
public static List<String> getRespondingInternetProtocolAddresses(final String subNetwork) throws IOException,
ExecutionException,
InterruptedException
{
final List<String> activeInternetProtocolAddresses = new ArrayList<>();
int startingIndex = 1;
int upperBound = 256;
int poolSize = upperBound - 1; // Query concurrently for best time savings
ExecutorService threadPool = Executors.newFixedThreadPool(poolSize);
List<Future<Runnable>> tasks = new ArrayList<>();
for (int currentSubNetIndex = startingIndex; currentSubNetIndex < upperBound;
currentSubNetIndex++)
{
final int subNetIndex = currentSubNetIndex;
// Query each Internet protocol address concurrently for speed purposes
Future task = threadPool.submit(new Thread(() ->
{
String currentInternetProtocolAddress = subNetwork + "." + subNetIndex;
try
{
if (Ping.isReachable(currentInternetProtocolAddress))
{
activeInternetProtocolAddresses.add(currentInternetProtocolAddress);
}
} catch (IOException exception)
{
exception.printStackTrace();
}
}));
tasks.add(task); // TODO Fix unchecked assignment warning
}
for (Future<Runnable> task : tasks)
{
task.get();
}
threadPool.shutdown();
return activeInternetProtocolAddresses;
}
在任务列表中添加新任务时,我收到了未经检查的分配警告:
tasks.add(task);
我尝试将 Future
by replacing it with Future<Runnable>
but it created a compile error instead since submit()
returns 泛化为 Future<?>
。
我该怎么做才能修复警告?
Future<T>
在应从任务返回类型 T
的某些结果时使用 - 然后您将获得此结果并使用它。
如果您只需要等到所有任务执行完毕,请使用 threadPool.submit(Runnable r)
。然后在 threadPool.shutdown()
调用 threadPool.awaitTermination()
之后——阻塞直到所有任务完成。
要解决此问题,您可以将任务声明为 List<Future<?>>
并将 task
声明为 Future<?>
。