[1] Ecclesiastes is a pretty great book to help figure out for yourself what exactly this is. It's not for nothing that the book uses the same word for "meaningless" or "empty" or "vanity" or "vapor" or "breath" as its refrain. It's also a beautiful book in itself as literature: https://en.wikipedia.org/wiki/Ecclesiastes#Influence_on_West...
(going really off topic here)
I was inspired to look at the Biblical version after reading the awesome “A Rose for Ecclesiastes” by Roger Zelazny, in which a poet on a mission to Mars uses it as a basis for communicating with the ancient Martian elders. Incidentally, a copy of Zelazny’s 1969 story was landed on Mars in 2008 on a DVD carried by the Phoenix probe [0]. If 'The Martian' was the perfect story of engineering on Mars, 'A Rose for Ecclesiastes' is perhaps the perfect poetic look at the planet, at least on a par with "The Silver Locusts".
[0] https://en.wikipedia.org/wiki/Phoenix_(spacecraft)#Phoenix_D...
First, know concentration is a skill, so develop that. I can tell you how to muscle up but if you can't pull up then knowing how won't help.
You are able to convince yourself you're interested in something by asking questions about it. Create the space in your brain that wants the knowledge to be in there. Find a question you genuinely want to know the answer to. There's usually at least something. This is how I was able to maintain focus on reading atomic physics, funnily enough, it's now the most prominent thing I remember from my degree.
For example, you start by always eating breakfast at a set time. After a few months you add going to sleep at a specific time. Once you regulate sleeping and eating you can add some exercise. After you learn how to control your physiological needs you will become more focused at work and daily life.
Remember to take things slowly.
Another poster mentioned it, but seeking mastery over the whims and desires of your mind is paramount. My anecdote above was basically that. My mind never stopped fighting me, begging me to be lazy and go play video games. But the inner acknowledgement that I had to succeed and graduate let me fight against it.
I also changed my environment, working out of coffee shops and computer labs, surrounded by other people studying and working their asses off. I joined the ACM chapter at my school and expanded my social network to include people who were working as hard as I wanted to. This provided mental sustenance when my personal discipline would wane.
I suggest listening and reading all of John Cleese's talks on creativity. The painful level of effort he describes is fundamentally related to these challenges.
Also, caffeine, copious amounts of it.
Then, he'll sit there, and after a few minutes, the boredom of doing nothing gets very strong, and he'll be motivated to write.
If you don't know yourself well enough to know what goal is truly important to you, you risk abandoning your task when the going gets tough. So self-knowledge is also important.
I think that's part of the reason why so many young people don't do that well in school.. they either don't know what they want or think they want one thing when they really don't. Once they mature, understand themselves better and so know what they really want then achieving it could become much easier (assuming they're not burnt out or too old by then).
Also, I'm not by any means a master at this, so I try to look to people who are, and learn from them. For me that means people who've been able to achieve amazing feats of endurance, like Robert Owens[1] and David Goggins[2], or have overcome some incredible adversity, like Brooke Ellison[3] and Kyle Maynard[4].
From some of them, I remember learning to focus on the present, on putting one foot in front of the other, in a kind of hyperfocus on exactly and only the very smallest thing you need to do right now, then doing it again, and again, and again, and again.
Another lesson I'm now remembering learning from many of them is that they built up a stock of achievements they could recall when the going got tough. Knowing that they persevered and succeeded before gave them confidence that they could do it again.
You also have to have hope, be willing to try (and try your best, no matter what the outcome), and somehow overcome depression and other mental issues, if you suffer from them. Therapy works for some, medication or other medical interventions work for others, religion for still others, but ultimately there are no sure or easy answers here.
Having a strong support network and mentors also really helps a lot. It's very difficult to go it alone, though some truly exceptional people manage it (though even then there's usually something in their early life that helped -- often parents or other sources of guidance)... I'm thinking of POW's and concentration camp survivors. Speaking of which, I can highly recommend Victor Frankl's Man's Search for Meaning,[5] as it's about why people endured in the absolute worst circumstances imaginable.
[1] - https://www.youtube.com/watch?v=Yg9kJeule7Q
[2] - https://www.youtube.com/watch?v=78I9dTB9vqM
[3] - https://www.youtube.com/watch?v=alf_I1p7bds
[4] - https://www.youtube.com/watch?v=jqRJ-178dVk
[5] - https://www.amazon.com/Mans-Search-Meaning-Viktor-Frankl/dp/...
Focus is a complex muscle and it's even worse when you're never challenged during childhood. 'grit', 'hard work' are empty words when you don't see the need. Breezing through school while being a smart ass annoying (childhood trauma will make you a pita to teach...) brat is no recipe to learn 'work'. It's different from physical work that was taught to me early, where you see the chai of effort to the result quite quickly, build a wall, a cement patio, wallpaper, paint, 'heavy' garden maintenance, housecleaning. Immediate or rapid feedback, you do you see.
With intellectual concentration, be it books, science lessons, etc. If there's no quick gratification ('oh I understand this immediately, new thing I know, cool' or 'Hu, interesting story') the voluntary focus muscle doesn't engage.
It took me years of searching at random how to make myself concentrate enough to 'go through' stuff.
I hope my kids won't have the 'panic-level fear of being poor' as a drive, but I also hope I find a way to teach them the value of sometimes forcing yourself without understanding why /right away/ (trusting you're on a path to enlightenment or victory or success...) and to teach them to exercise the focus muscle as they need.
This one really resonates with me. During a difficult period when I was struggling with burnout and feeling "scatterbrained", I developed a habit of writing down the next task I needed to do. If I couldn't bring myself to make any progress on it, I would then break it down and write down the first subtask, etc. (I would not write down a big list of subtasks as this would only discourage me, it was just about the next one or two things.) On a bad day this might recurse down to extremely simple things like "connect to the VPN" or "open the visual studio project", but I'd write it down and checkmark it nonetheless to force myself to make minimal progress. For each top level task, I would end up with a OneNote page with multiple screenfuls of deeply indented notes and ctrl+1 checkboxes everywhere.
Additional benefits of this approach include the ability to continue quickly after interruptions or distractions, and to easily switch between tasks when waiting is involved. It also helps to dig deeper into tasks (e.g. really understand each change in a code review instead of glossing over big chunks in the diff). So I've continued this habit ever since.