This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void preloadDataInCache(final NamedCache cache, Collection keys) { | |
// cache.getAll(keys); | |
int threadCount = 32; | |
ExecutorService threads = Executors.newFixedThreadPool(threadCount); | |
Set[] subsets = new Set[threadCount]; | |
int n = 0; | |
for(Object key: keys) { | |
int i = (n++) % subsets.length; | |
if (subsets[i] == null) { | |
subsets[i] = new HashSet(); | |
} | |
subsets[i].add(key); | |
} | |
Future[] futures = new Future[threadCount]; | |
for(int i = 0; i != threadCount; ++i) { | |
final Set subset = subsets[i]; | |
if (subset != null) { | |
futures[i] = threads.submit(new Runnable() { | |
@Override | |
public void run() { | |
cache.getAll(subset); | |
} | |
}); | |
} | |
} | |
for(int i = 0; i != threadCount; ++i) { | |
if (futures[i] != null) { | |
try { | |
futures[i].get(); | |
} catch (Exception e) { | |
// ignore for simplicity | |
} | |
} | |
} | |
threads.shutdown(); | |
// code above is just illustration not a production quality code | |
} |
Comments