Improved code for waiting until all executed tasks will be done (in thread pool)