Java Multithreading

Java offers built-in support for the multithreaded programming. A multithreaded programs contains two or more parts that can run at the same time. Each part of such a program is called a thread, and each thread defines a separate path of execution. Therefore, multithreading is a special form of multitasking.

You are almost certainly acquainted with multitasking because it is supported by virtually every modern operating systems. However, there are two distinct types of multitasking given here:

  1. process-based multitasking
  2. thread-based multitasking

It is important to understand about the difference between the above two.

Process-Based Multitasking

For many readers, process-based multitasking is more companion form. A process is, a program that is executing. Therefore, a process-based multitasking is the feature which allows your computer to run two or more programs at the same time. For instance, process-based multitasking allows you to run the Java compiler concurrently which you are using a text editor or visiting a website. In the process-based multitasking, a program is the smallest building block of code which can be dispatched by the scheduler.

Thread-Based Multitasking

In thread-based multitasking environment, the thread is the smallest unit of dispatchable code. It means that a thread-based multitasking program can execute two or more tasks at the same time. For instance, a text editor can format the text concurrently that it is printing, as long as these two actions are being executed by the two separate threads. Therefore, the process-based multitasking handles with the "big picture", and the thread-based multitasking handles the details.

Multithreading

Multithreading provides you to write efficient programs that makes maximum use of the processing power available in the system. One crucial way multithreading achieves this is by keeping the idle time to a minimum. This is especially crucial for the interactive, networked environment in which Java operates because idle time is common. For example, the transmission rate of the data over a network is much slower than the rate at which the computer can process it. Yet the local file system resources are read and written at a much slower pace than they can be processed by the CPU. And user input is much slower than the computer. In a single-threaded environment, your program has to wait for each of these tasks to end before it can proceed to the next one, even though most of the time the program is idle, waiting for the input. Multithreading allows you to reduce this idle time because another thread can run when one is waiting.

If you have already programmed for operating systems such as Windows, then you are already familiar with multithreaded programming. Nevertheless, the fact that Java manages threads makes multithreading especially convenient because many of the details are handled for you.

Java Online Test


« Previous Tutorial Next Tutorial »