Provide Best Programming Tutorials

Introduction to multithread programming


Multithreading enables multiple tasks in a program to be executed concurrently.One of the powerful features of Java is its built-in support for multithreading—the concurrent running of multiple tasks within a program. In many programming languages, you have to invoke system-dependent procedures and functions to implement multithreading. This chapter introduces the concepts of threads and how multithreading programs can be developed in Java.

Thread Concepts

A program may consist of many tasks that can run concurrently. A thread is the flow of execution, from beginning to end, of a task.

A thread provides the mechanism for running a task. With Java, you can launch multiple

threads from a program concurrently. These threads can be executed simultaneously in multiprocessor

systems, as shown in Figure a


  • (a) Here multiple threads are running on multiple CPUs.
  • (b) Here multiple threads share a single CPU.

In single-processor systems, as shown in Figure b, the multiple threads share CPU time, known as time sharing , and the operating system is responsible for scheduling and allocating resources to them. This arrangement is practical because most of the time the CPU is idle. It does nothing, for example, while waiting for the user to enter data.

Multithreading can make your program more responsive and interactive, as well as enhance performance. For example, a good word processor lets you print or save a file while you are typing. In some cases, multithreaded programs run faster than single-threaded programs even on single-processor systems.

Leave a Reply

Close Menu