Originally posted by Sahil Malkani: What exactly is a Thread?I know it is used for multiple process execution! But how would one define it?How does it really help in Simultaneous execution of code? I just want to understand the basics!
You can start here with the tutorial from the Sun website.
To briefly answer your question, multi-threaded applications have advantages over single threading since:
- Can be adapted to systems with more than one processor easily, whereas truly single threaded applications running on a multiple-processor machine/system will only use one processor at a time.
- Its difficult for a thread to use 100% of the CPU during its entire life cycle. For example, anytime data needs to be loaded from disk (even RAM), the thread will pause and not use any CPU time while reading the data. This leads to a lot of wasted time, so having other threads to keep the CPU busy is prefable.
The only time multi-threading does not work is if you are constantly using 100% CPU and the code is extremely interdependant, ie complex Mathmatics code where each equation depends on the previous work. [ November 29, 2005: Message edited by: Scott Selikoff ]
process based multitasking and thread based multitasking.
In process based multitasking, two processes run concurrently on an operating system. For example, we can think of a word processor and an excel sheet running at the same time in parallel. These are two different programs that do not share common memory space ( in other words, they have separate data and code ) but they are just running in parallel.
Thread based multitasking is at a finer level than this. Two threads share the same memory space, they can operate on same data and they are usually parts of the same program. They have different execution paths and hence can run concurrently making an efficient use of operating system.
I would suggest to read the Sun Tutorial on threads, its excellent to begin with. When you feel like you have sufficient knowledge of threads, try writing some code on your own, that would give you first-hand experience.