Multithreading - RichardDanielOliva/java-learning-wiki GitHub Wiki

Threads allow multiple actions to be performed at the same time inside a single process.

When you have a machine with multiple cores, you can run multiple tasks at the same time. In programming, a single process can have multiple threads working at the same time. Like a process, a thread is an independent path of execution that runs in isolation. Each thread has its own stack and its own local variables so when a method is running on a thread, the local variables in the method are only available within that thread.

Process vs thread

Threads are more closely connected to each other than separate processes. When you open an email app on your computer and then open a browser window, those things are separate processes and have nothing to do with each other. But threads inside the same process are more closely connected, they share memory with other threads. All of the threads have the same access to global variables. This can sometimes lead to issues and mean that care should be taken when using multiple threads.

Java API

Java has its own API for handling threads which is actually quite simple to use, but care should be taken when using threads in complex applications. Examples of when threads can be useful in Java include: Using blocking input and output functions. For example, if the program asks the user to input some data, a read method might be used which blocks the program until the user has input something. This might prevent the program from performing other tasks while waiting for the user. If they try to do something else, like click on a button, nothing would happen. If the program has a graphical user interface, and especially if they use toolkits such as AWT and Swing, multi-threading can be used to improve the responsiveness of the program and stop it from hanging. In particular, if there is a large and complex program that performs independent tasks, using multi-threading is often the most elegant solution. It is now much more common than it used to be for machines to have multiple processes and multi-threading allows you to take advantage of that.