Session 14 |
Thread concepts
One of the powerful features of Java is its built-in support for multithreading — the concurrent running of multiple tasks within a program. A program may consists of many tasks that can run concurrently. A thread is the flow of the task execution from its beginning to the end.
A thread provides the mechanism for running a task. With Java, we can launch multiple threads. These threads can be executed simultaneously in multiprocessor systems. In single-processor systems the multiple threads share CPU time, known as time sharing, and the operating system is responsible for scheduling and allocating resources to them.
Multithreading can make our program more responsive and interactive, as well as enhance performance. For example, a good word processor let us print or save a file while we are typing. In some cases, multithreaded programs run faster than single-threaded programs even on single-processor systems. Java provides exceptionally good support for creating and running threads and for locking resources to prevent conflicts.
We can create additional threads to run concurrent tasks in the program. In Java, each task
is an instance of the Runnable
interface, also called a runnable object. A thread is an object that facilitates the execution of the task.
Session 14 |