The phrase “to hang by a thread” means: To be subject to imminent danger; to be in a hazardous position or precarious condition.
The allusion is to one Damocles, a courtier in the reign of Dionysius of Syracuse in the fifth century B.C.
All we know of this courtier is from the tale told both by Cicero and by Horace.
He was given to extreme flattery of his ruler, and, one day, having praised extravagantly the power of Dionysius was invited to see for himself just how much happiness that power brought.
Accordingly he was given a magnificent feast, and was surrounded by luxury and entertainment beyond description.
In the midst of this, however, he happened to glance above him and saw, suspended by a single hair, a naked sword pointed directly over his head.
The intent, of course, was to show that a king, perhaps even more than his subjects, could never enjoy unalloyed happiness.
We also refer to the sword of Damocles as a symbol of impending doom or threat of danger.