Could be that the CPU isn't fully seated and thermal expansion / contraction is causing an intermittent connection on some CPU pins. If that's the case, re-seating the CPU should do it.
It could be that the CPU is overheating... and you may not even know it. If your heat sink isn't seated properly you likely won't even hear your fans spinning up; if your thermal sensor is malfunctioning the computer has no way of knowing if the CPU is overheating; or if you're using a heat-pipe heatsink and the heatpipe has failed it could be that it's not removing enough heat from the CPU. Testing and reseating the heatsink is called for in this case. Test it by removing the heatsink, dipping the base of it in hot water, then feeling the heat pipe or fins... heat should rapidly move from the hot water to the heat pipe or fins. If it doesn't, replace the heat sink. When reseating the heat sink, I recommend diamond heat sink compound, as diamonds have extremely high heat transfer capability.
It could also be that thermal creep has caused some of the connections to the socket for the CPU to start opening... when they first started using lead-free solder, this was a bigger issue. A reflow of the solder is called for in this case. I won't go into that, as it entails baking your motherboard at a very precise temperature and the risk of ruining your motherboard is high.
It could be a power supply issue... if your power supply's capacitors are starting to go bad, they could be sending bad power to the motherboard. This was more of an issue about 15 years ago when a bunch of cheap counterfeit capacitors made their way into the market. Check all the caps on your motherboard and in your power supply... if you see the top puffed up, you know it's going bad. Unsoldering the old cap and soldering in new is called for in this case.
It could be a power supply issue... if you've added a card, drive or peripheral lately, you could be pushing up against the capacity of your power supply. Buying a higher-capacity power supply is called for in this case.
It could be a power supply issue... if your wall power is dirty, that could be making its way through the power supply's filtering and corrupting data, leading to spontaneous reboots. A power supply that filters better, or an always-on UPS (one that inverts the power from the battery into a sine wave at all times, not just when the power is out; and separately charges the battery from wall power) is called for in this instance... the battery, then, absorbs any wall power glitches.
It could be bad memory... a memory check is called for in this instance.
It could be a programming glitch... if you've got a driver installed that's not exactly the one you need, it could cause this. Updating the system is called for in this case.
It could be lack of sufficient memory... if you've got only a small amount of memory and the default 2GB of swap and you attempt to run a lot of programs, the kernel could panic. Installing no-hang is called for, as well as getting more memory, as well as setting up a larger swap space.
nohang/focal,now 0.2.0-1~oibaf~f all
sophisticated low memory handler for Linux
sudo add-apt-repository ppa:oibaf/test
sudo apt update
sudo apt install nohang
sudo systemctl enable --now nohang-desktop.service
It could be bad sectors on your drive... if the data's not saved accurately, your data could be getting corrupted, or the computer glitches after the drive times out after attempting to read or write data to a bad sector. Checking all the drives for errors is called for in this case.