Some desktop Operating System (OS) do spontaneously switch focus among applications without the user being aware of it. A typical issue is when you are typing in text editor and suddenly another app comes to foreground without warning and your keystrokes are directed to it. It often happens when an application is launched in the background and takes time to load up. Or when you load a large file in an app, then switch to another and a minute or so later the first app grab the focus because the file is loaded.
I find the practice not only annoying but I also think it is a security risk. For instance if you are entering a password and suddenly another app gets the focus it is easy to leak confidential information. Another risk is when the keystrokes intended for one app actually trigger unwanted actions in another (e.g. delete folder in a file manager).
I know MS Windows and Ubuntu have this issue, not sure about Mac OS and others. You can find questions on SO where people ask for work-around on their specific OS so it seems I'm not the only one finding this default behaviour irritating.
I'd be interested to know why OSes have this default behaviour despite the drawbacks explained above and why they don't seem to offer built-in config settings to disable it?
Another related question is if there are existing UX patterns that could be used to solve this issue? One I can think of is a temporary message box in a corner of the screen to attract attention to an application, just like an email client does when a new email has been received.