Why use multithreading?
17 December 2007In this post, I will talk about the importance of multithreading and would like to answer the question: Why use multithreading ??
Multithreading exploits the fact that most of the time the tasks (parts) of the same program are either waiting for the other resources to become free, or waiting for some timeout to occur. In the above example (spreadsheet), scroll operation is waiting for the calculation to be completed. If these parts or tasks can be described as independent threads, they can run individually and they don’t have to wait for the other threads to be completed. One can automatically switch from one task that is ready to wait to another task that is ready. In multithreading, two different tasks – performs calculations and scrolling, form two different threads. When the first thread performs calculation, the second thread can make the window scroll down. This would give the notion of both the operation being performed simultaneously to the user and improves the effectiveness of the application.
Multithreading can be done in a single-processor or multiple processor environments. In a multiple processor environment, different threads can run on different processors.
In a single-processor system, however the things happen in a different way. In such a system, multiple threads share the CPU time. The operating system is responsible for scheduling and allocating resources to threads. This arrangement is very practical as a thread does not always need the CPU time. It might have to wait for user input or it might have to display something on the screen. During that time, the other thread takes over the CPU. The previous thread can then resume after the current thread has either utilized all CPU time or the thread has ended.
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: Why use multithreading?
Hi
I want to understand one thing that..
jvm in an application server creates threads as and when request comes. There are thread pool settings and it takes care of it.
Then why do we need to worry about to make our code multi threaded so as an application server works better in a multicore environment?
Thanks in advance