java - Fix Future Unchecked Assignment Warning -
i have code pings ip addresses in given sub network. using concurrency improve performance since waiting timeout every ip address take longer otherwise:
/** * @param subnetwork subnet scan * @return list of internet protocol addresses 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 best time savings executorservice threadpool = executors.newfixedthreadpool(poolsize); list<future<runnable>> tasks = new arraylist<>(); (int currentsubnetindex = startingindex; currentsubnetindex < upperbound; currentsubnetindex++) { final int subnetindex = currentsubnetindex; // query each internet protocol address concurrently 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 } (future<runnable> task : tasks) { task.get(); } threadpool.shutdown(); return activeinternetprotocoladdresses; }
when adding new task tasks list, i'm getting unchecked assignment warning:
tasks.add(task);
i tried generify future
replacing future<runnable>
created compile error instead since submit()
returns future<?>
.
what can fix warning?
to solve can declare tasks list<future<?>>
, task
future<?>
.
Comments
Post a Comment