replaced time measuring by time measuring based on running state of a threaf