added code for adaptively sleeping threads