Added lock based threading algorithm