He called the mosque in Queens and left threatening messages, and when a door was smashed in, the man was also the culprit, the authorities said. Then, early one morning in 2012, prosecutors said, the same man waited as a worshiper unlocked the mosque’s front door and then stabbed the worshiper repeatedly.

On Wednesday, the man, Bernhard Laufer, was sentenced to 20 years in prison after he was found guilty of several charges that had been prosecuted as hate crimes, prosecutors said.

Mr. Laufer, 59, had been convicted in State Supreme Court in Queens of second-degree attempted murder, first-degree attempted assault, second-degree assault, criminal possession of a weapon and criminal mischief.

Mr. Laufer “waged a war of terror against this mosque,” Richard A. Brown, the Queens district attorney, said in a statement. The man stabbed by Mr. Laufer, he added, was left with “permanent scars and a daily reminder of the heinous, unprovoked attack.”