Fixed problems with while loop for each thread

Change-Id: Ib02e932979fc5382a6d89a338da50717d320898a