Patiently waiting for the thread to end