There is a secret to Japan’s long-lasting dynasty.
One. Keep it in the family.
In the 700s, during the Nara period, there was an Empress (never married) who was having an affair with a top Buddhist priest, and was apparently plotting to have this priest succeed her as Emperor. The plot failed when she passed away too soon, and the ruling class sent the bonze packing.
But the Japanese elites were so shocked by this that women were not allowed to ascend the throne for another 900 years (there had been a few before her, but the next was in the 17th century, and in modern Japan the rules do not allow women rulers at all). It was also the spur for the Imperial House to abandon Nara for Heian-Kyo (modern Kyoto) within a few years after that time (to escape the baleful influence of corrupt Buddhist priests, but of course they merely followed them to Kyoto…)
Two. Let someone else wield the real power.
One thing about the Imperial House is that, after the early days, the reigning emperor was usually a placeholder with the real power being wielded by someone else.
Early on, this was usually someone else in the Imperial household. The Fujiwara family, for example, often ran things from the late 7th century on, wielding power by marrying daughters off to prospective young emperors (hence, father-in-law in charge).
Later on, the cloistered Emperor system arose, where an emperor would retire, cede the throne to a son or nephew, and continue running things from behind the scenes (often taking the tonsure to become a Buddhist priest, and ruling from the grounds of a Buddhist temple). By the 11th or 12th century this was so common that there might be two or three such cloistered emperors at once.
Then, in the latter 12th century, samurai houses ascended (these were descendants of families that had split off from aristocratic imperial houses in earlier centuries), and began to run things in place of the Emperor. At first they were in Kyoto, but then shifted to Kamakura in the 13th century.
This behind-the-scenes rulership soon got out of hand, because the Fujiwara were (believe it or not) still around, as was the cloistered emperor system, with the Shogun behind them. Because (sure enough) the shogun had a power behind HIM. The Minamoto shogunal clan was soon dominated by the family of the founder’s wife, the Hojo, who called themselves Regent. And within a few generations the Regent was dominated by YET ANOTHER behind-the-scenes office.
So by around 1300, there were something like five levels of nested rulers in Japan (Fujiwara, cloistered Emperor (maybe plural), Shogun, Regent, Regent to the Regent, etc.).
This was getting ridiculous, and it did get pared back somewhat in later centuries. But the shogunal system remained in effect down to the latter 19th century, when the Tokugawa at last gave way to an Imperial restoration, and the Emperor moved court to Edo (renamed Tokyo).
But did he resume real power? No he did not. Not really, because he became a sort-of European-style constitutional monarch thereafter.
So this is how the dynasty has survived all these centuries: Keep it away from the real centers of power, and no one will bother trying to make it go extinct.
Note: This glosses over some details, of course. But Japan’s history is REALLY long, so you have to skim over some of it…