ensure all threads end and fix NPE

1 When there exception happens, main function exits but
some submitted tasks will run and get cleared
local variables, which will cause NPE. So change the
condition to wait all submitted tasks exit.
2 When there uncatched exception happens in the
ThreadPoolExecutor, current thread will be destoryed
and new thread will be created, which will cause index
out of bounds. So add bound check.

Change-Id: I9a8360efadddfb0cac8bba4c746754a4d7886363
CRs-Fixed: 2223180
1 file changed