@Test public void getQuantityString_onDemandLoading_calledFromMultipleThreads() throws InterruptedException { int numberOfThreads = 100; String[] expected = {"零个", "一个", "两个", "少许", "多数", "其他"}; int numQuantityStrings = expected.length; ExecutorService service = Executors.newFixedThreadPool(numberOfThreads); CountDownLatch latch = new CountDownLatch(numberOfThreads); Callable<String[]> getQuantityStringTask = () -> { String[] result = new String[numQuantityStrings]; for (int i = 0; i < numQuantityStrings; i++) { result[i] = parsedStringPack.getQuantityString(PLURALS_ID, (long) i, TEST_PLURAL_RULES); } latch.countDown(); return result; }; List<Callable<String[]>> tasks = new ArrayList<>(); for (int i = 0; i < numberOfThreads; i++) { tasks.add(getQuantityStringTask); } 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 { String[] actual = result.get(); for (int j = 0; j < numQuantityStrings; j++) { assertEquals("Correct plural not fetched", expected[j], actual[j]); } } catch (ExecutionException ee) { assertTrue("Exception when fetching plurals " + ee.toString(), false); } } }
@Nullable public String getQuantityString(int id, Object quantity, @NonNull PluralRules pluralRules) { String[] plural = plurals.get(id); if (plural == null) { plural = loadPlural(id); } if (plural == null) { return null; } final int index = quantityIndex(pluralRules.quantityForNumber(quantity)); String result = plural[index]; if (result != null) { return result; } return plural[0]; }
ParsedStringPack { @Nullable public String getQuantityString(int id, Object quantity, @NonNull PluralRules pluralRules) { String[] plural = plurals.get(id); if (plural == null) { plural = loadPlural(id); } if (plural == null) { return null; } final int index = quantityIndex(pluralRules.quantityForNumber(quantity)); String result = plural[index]; if (result != null) { return result; } return plural[0]; } }
ParsedStringPack { @Nullable public String getQuantityString(int id, Object quantity, @NonNull PluralRules pluralRules) { String[] plural = plurals.get(id); if (plural == null) { plural = loadPlural(id); } if (plural == null) { return null; } final int index = quantityIndex(pluralRules.quantityForNumber(quantity)); String result = plural[index]; if (result != null) { return result; } return plural[0]; } ParsedStringPack(@NonNull InputStream inputStream, @NonNull List<String> parentLocales); }
ParsedStringPack { @Nullable public String getQuantityString(int id, Object quantity, @NonNull PluralRules pluralRules) { String[] plural = plurals.get(id); if (plural == null) { plural = loadPlural(id); } if (plural == null) { return null; } final int index = quantityIndex(pluralRules.quantityForNumber(quantity)); String result = plural[index]; if (result != null) { return result; } return plural[0]; } 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 getQuantityString(int id, Object quantity, @NonNull PluralRules pluralRules) { String[] plural = plurals.get(id); if (plural == null) { plural = loadPlural(id); } if (plural == null) { return null; } final int index = quantityIndex(pluralRules.quantityForNumber(quantity)); String result = plural[index]; if (result != null) { return result; } return plural[0]; } 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);  }