AsynHelper जावा पुस्तकालय में ऐसी अतुल्यकालिक कॉल (और प्रतीक्षा) के लिए उपयोगिता वर्गों / विधियों का एक सेट शामिल है।
यह अतुल्यकालिक रूप से विधि कॉल या कोड ब्लॉक का एक सेट चलाने के लिए वांछित है, तो यह एक उपयोगी सहायक विधि शामिल AsyncTask .submitTasks टुकड़ा नीचे के रूप में।
AsyncTask.submitTasks(
() -> getMethodParam1(arg1, arg2),
() -> getMethodParam2(arg2, arg3)
() -> getMethodParam3(arg3, arg4),
() -> {
//Some other code to run asynchronously
}
);
यदि यह सभी अतुल्यकालिक कोडों को पूरा होने तक प्रतीक्षा करने के लिए वांछित है, तो AsyncTask.submitTasksAndWait वैरिएंट का उपयोग किया जा सकता है।
इसके अलावा अगर यह अतुल्यकालिक विधि कॉल या कोड ब्लॉक में से प्रत्येक से एक रिटर्न वैल्यू प्राप्त करने के लिए वांछित है, तो AsyncSupplier .submitSuppliers का उपयोग किया जा सकता है ताकि परिणाम फिर से प्राप्त किया जा सके परिणाम आपूर्तिकर्ता सरणी विधि द्वारा लौटाया जा सकता है। नीचे नमूना स्निपेट है:
Supplier<Object>[] resultSuppliers =
AsyncSupplier.submitSuppliers(
() -> getMethodParam1(arg1, arg2),
() -> getMethodParam2(arg3, arg4),
() -> getMethodParam3(arg5, arg6)
);
Object a = resultSuppliers[0].get();
Object b = resultSuppliers[1].get();
Object c = resultSuppliers[2].get();
myBigMethod(a,b,c);
यदि प्रत्येक विधि का रिटर्न प्रकार भिन्न होता है, तो नीचे दिए गए स्निपेट का उपयोग करें।
Supplier<String> aResultSupplier = AsyncSupplier.submitSupplier(() -> getMethodParam1(arg1, arg2));
Supplier<Integer> bResultSupplier = AsyncSupplier.submitSupplier(() -> getMethodParam2(arg3, arg4));
Supplier<Object> cResultSupplier = AsyncSupplier.submitSupplier(() -> getMethodParam3(arg5, arg6));
myBigMethod(aResultSupplier.get(), bResultSupplier.get(), cResultSupplier.get());
अतुल्यकालिक विधि कॉल / कोड ब्लॉक का परिणाम एक ही धागे में कोड के एक अलग बिंदु पर या नीचे के स्निपेट में एक अलग धागे के रूप में भी प्राप्त किया जा सकता है।
AsyncSupplier.submitSupplierForSingleAccess(() -> getMethodParam1(arg1, arg2), "a");
AsyncSupplier.submitSupplierForSingleAccess(() -> getMethodParam2(arg3, arg4), "b");
AsyncSupplier.submitSupplierForSingleAccess(() -> getMethodParam3(arg5, arg6), "c");
//Following can be in the same thread or a different thread
Optional<String> aResult = AsyncSupplier.waitAndGetFromSupplier(String.class, "a");
Optional<Integer> bResult = AsyncSupplier.waitAndGetFromSupplier(Integer.class, "b");
Optional<Object> cResult = AsyncSupplier.waitAndGetFromSupplier(Object.class, "c");
myBigMethod(aResult.get(),bResult.get(),cResult.get());