Thread iteration logging and optimizing waiting in threads