Lock when changing thread state