The Talmud (Shabbat 30a) relates the following interesting tale about King David's death:
And as to what Solomon said, 'for a living dog is better than a dead lion', — that is as Rab Judah said in Rab's name, viz.; what is meant by the verse, Lord, make me to know mine end, and the measure of my days, what it is; let me know how frail I am. David said before the Holy One, blessed be He, 'Sovereign of the Universe! Lord, make me to know mine end.' 'It is a decree before Me,' replied He, 'that the end of a mortal is not made known.' 'And the measure of my days, what it is'-'it is a decree before Me that a person's span [of life] is not made known.' 'Let me know how frail [hadel] I am.' Said He to him. 'Thou wilt die on the Sabbath.' 'Let me die on the first day of the week!' 'The reign of thy son Solomon shall already have become due, and one reign may not overlap another even by a hairbreadth.' 'Then let me die on the eve of the Sabbath!' Said He, 'For a day in thy courts is better than a thousand': better is to Me the one day that thou sittest and engagest in learning than the thousand burnt-offerings which thy son Solomon is destined to sacrifice before Me on the altar.' Now, every Sabbath day he would sit and study all day. On the day that his soul was to be at rest, the Angel of death stood before him but could not prevail against him, because learning did not cease from his mouth. 'What shall I do to him?' said he. Now, there was a garden before his house; so the Angel of death went, ascended and soughed in the trees. He [David] went out to see: as he was ascending the ladder, it broke under him. Thereupon he became silent [from his studies] and his soul had repose.
God clearly told David that he would die on Shabbat. Yet David attempted to save himself by always studying Torah all Shabbat. How could he have thought that he could outsmart God and keep himself alive?
This is all the more astounding because it's not just that God told him that he would die on Shabbat (in which case you might think that it was not set in stone and could perhaps be changed). David specifically asked God to alter the plan and God explicitly told David that He would under no circumstances alter the plan, as King Solomon's reign could not be pushed off even for a moment. And in fact, as the story shows, David's plan did not succeed.
Knowing all this, what did King David think he could actually accomplish by studying Torah all Shabbat?