avoid continous blocking from the thread cleanup wait loop

Modified code block previously waited infinitely until the thread pool reaches a safe threshold (e.g. 1000) from MAX (5000). Even though chances are less for the thread pool to not clear up for a long time, since it occurs in current build, I have updated the loop to give up after 5 attempts to avoid blocking other tasks.(THREADPOOL_RECOVERY_ATTEMPTS)