I have heard Java 8 provides a lot of utilities regarding concurrent computing. Therefore I am wondering what is the simplest way to parallelise the given for loop?
public static void main(String[] args)
{
Set<Server> servers = getServers();
Map<String, String> serverData = new ConcurrentHashMap<>();
for (Server server : servers)
{
String serverId = server.getIdentifier();
String data = server.fetchData();
serverData.put(serverId, data);
}
}
Read up on streams, they're all the new rage.
Pay especially close attention to the bit about parallelism:
So to recap, there are no parallel for-loops, they're inherently serial. Streams however can do the job. Take a look at the following code: