@Test public void getString_onDemandLoadingSameString_calledFromMultipleThreads() throws InterruptedException { int numberOfThreads = 100; String expected = "你好，世界"; ExecutorService service = Executors.newFixedThreadPool(numberOfThreads); CountDownLatch latch = new CountDownLatch(numberOfThreads); Callable<String> getStringTask = () -> { String string = parsedStringPack.getString(STRING_ID); latch.countDown(); return string; }; List<Callable<String>> tasks = new ArrayList<>(); for (int i = 0; i < numberOfThreads; i++) { tasks.add(getStringTask); } List<Future<String>> results = service.invokeAll(tasks, 2, TimeUnit.SECONDS); latch.await(); Thread.sleep(2000); for (int i = 0; i < numberOfThreads; i++) { Future<String> result = results.get(i); assertTrue(!result.isCancelled()); try { assertEquals("Problem fetching correct string", expected, result.get()); } 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);  }