Unlike most other computer languages, java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution. Thus, multithreading is a specialized form of multitasking. You are certainly acquainted with multithreading, because it is supported by virtually all modern operating systems. However, there are two distinct types of multitasking – process based and thread based. It is important to understand the difference between the two. Process based multitasking is the more familiar form. A process is, in essence, a program that is executing. Thus, process-based multitasking is the feature that allows your computer to run two or more programs concurrently. For example process-based multitasking enables you to run the java compiler at the same time that you are using a text editor. In process based multitasking a program is the smallest unit of code that can be dispatched by the scheduler.
In a thread-based multitasking environment, the thread is the smallest unit of dispatch able code. This means a single program can perform two or more tasks simultaneously. For instance, a text editor can format text at the same time that it is printing, as long as these two actions are being performed by two separate threads. Thus process based multitasking deals with the “big picture” and thread based multitasking handles the details.
Multitasking threading requires less overhead than multitasking processes. Processes are heavy weight task that requires their own separate address space. Interprocess communication is expensive and limited. Context process from one switching to another is also costly. Threads, on other hand are light weight. They share the same address space and cooperatively share the same heavy weight process. Inter thread communication is inexpensive and context switching from one thread to next is low cost. While java program make use of process based multitasking environments, process based multitasking is not under the control of java. However, multithread tasking is.
Multithreading enables you to write very efficient program that make maximum use of CPU, idle time can be kept minimum. This is especially important to interactive, networked environment in which java operates, because idle time is common. For example, the transmission rate of a data over a network is much slower over the rate that which computer can process it. Even the local file system resources are read and written as much slower pace than they can be processed by the CPU. And of course user input is slower the computer. In traditional single threaded environment your computer has to wait for each tasks to finish before it can proceeds to the next one-even though the CPU is sitting in the idle most of the time. Multithreading lets you to gain access you to the idle time and lets you to make best use of it.
If you have programming for operating systems such as windows 98 and windows 2000, then you are already familiar with multithreading programming. However, the fact that java manages threads makes multitasking especially convenient, because many of details handles by you.