@Test public void getString_onDemandLoadingMultipleStrings_calledFromMultipleThreads() throws InterruptedException { int numberOfThreads = 100; int numStringToCall = EXPECTED_STRINGS.length; ExecutorService service = Executors.newFixedThreadPool(numberOfThreads); CountDownLatch latch = new CountDownLatch(numberOfThreads); List<Callable<List<String>>> tasks = new ArrayList<>(); Random random = new Random(); for (int i = 0; i < numberOfThreads; i++) { final int nextRandomNumber = random.nextInt(numStringToCall - 1) + 1; tasks.add(() -> { List<String> outcome = new ArrayList<>(); outcome.add(EXPECTED_STRINGS[nextRandomNumber]); outcome.add(parsedStringPack.getString(nextRandomNumber)); latch.countDown(); return outcome; }); } List<Future<List<String>>> results = service.invokeAll(tasks, 5, TimeUnit.SECONDS); latch.await(); Thread.sleep(2000); for (int i = 0; i < numberOfThreads; i++) { Future<List<String>> result = results.get(i); assertTrue(!result.isCancelled()); try { List<String> output = result.get(); assertEquals("Problem fetching correct string", output.get(0), output.get(1)); } catch (ExecutionException ee) { assertTrue("Exception when fetching string " + ee.toString(), false); } } }
@Nullable public String getString(int id) { final String result = strings.get(id); if (result == null) { return loadString(id); } else { return result; } }
ParsedStringPack { @Nullable public String getString(int id) { final String result = strings.get(id); if (result == null) { return loadString(id); } else { return result; } } }
ParsedStringPack { @Nullable public String getString(int id) { final String result = strings.get(id); if (result == null) { return loadString(id); } else { return result; } } ParsedStringPack(@NonNull InputStream inputStream, @NonNull List<String> parentLocales); }
ParsedStringPack { @Nullable public String getString(int id) { final String result = strings.get(id); if (result == null) { return loadString(id); } else { return result; } } ParsedStringPack(@NonNull InputStream inputStream, @NonNull List<String> parentLocales); boolean isEmpty(); @Nullable String getString(int id); @Nullable String getQuantityString(int id, Object quantity, @NonNull PluralRules pluralRules); }
ParsedStringPack { @Nullable public String getString(int id) { final String result = strings.get(id); if (result == null) { return loadString(id); } else { return result; } } ParsedStringPack(@NonNull InputStream inputStream, @NonNull List<String> parentLocales); boolean isEmpty(); @Nullable String getString(int id); @Nullable String getQuantityString(int id, Object quantity, @NonNull PluralRules pluralRules);  }