0% found this document useful (0 votes)
0 views

DirectFileTopicDownload-2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

DirectFileTopicDownload-2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

Chapter 12

Multithreaded Programming
Multithreading Fundamentals
There are two distinct types of multitasking: process-based and thread-based.

Process-based multitasking is the feature that allows your computer to run two
or more programs concurrently.

For example, it is process-based multitasking that allows you to run the Java
compiler at the same time you are using a text editor or browsing the Internet. 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


dispatchable code. This means that a single program can perform two or more
tasks at once. For instance, a text editor can be formatting text while it is printing,
as long as these two actions are being performed by two separate threads.
Multithreading Fundamentals…
A principal advantage of multithreading is that it enables you to write
very efficient programs because it lets you utilize the idle time that is
present in most programs.

As you probably know, most I/O devices, whether they be network ports,
disk drives, or the keyboard, are much slower than the CPU. Thus, a
program will often spend a majority of its execution time waiting to send
or receive information to or from a device.

By using multithreading, your program can execute another task during


this idle time. For example, while one part of your program is sending a
file over the Internet, another part can be reading keyboard input, and
still another can be buffering the next block of data to send.
Multithreading Fundamentals…
Java’s multithreading features work in two types of systems.

In a single-core system, concurrently executing threads share the CPU, with


each thread receiving a slice of CPU time.

Therefore, in a single-core system, two or more threads do not actually run at


the same time, but idle CPU time is utilized.

However, in multiprocessor/multicore systems, it is possible for two or more


threads to actually execute simultaneously. In many cases, this can further
improve program efficiency and increase the speed of certain operations.
Multithreading Fundamentals…
A thread can be in one of several states. It can be running. It
can be ready to run as soon as it gets CPU time. A running
thread can be suspended, which is a temporary halt to its
execution. It can later be resumed. A thread can be blocked
when waiting for a resource. A thread can be terminated, in
which case its execution ends and cannot be resumed.

Along with thread-based multitasking comes the need for a


special type of feature called synchronization, which allows
the execution of threads to be coordinated in certain well-
defined ways. Java has a complete subsystem devoted to
synchronization
MULTITHREADING - 2

You might also like