Advanced Java Programming- new 2
Advanced Java Programming- new 2
Multithreading
Java provides built-in support for
multithreaded programming. A multithreaded
program contains two or more parts of
program that can run concurrently.
Each part of a program is called a thread, and
each thread defines a separate path of
execution.
There are two distinct types of multitasking:
i) Process based and ii) Thread-based.
A process is part of a program that is
executing. Thus, process-based multitasking
is the feature that allows our computer to run
two or more programs concurrently.
For example, process-based multitasking
enables us to run the Java compiler at the
same time that we are using a text editor.
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 simultaneously.
For example, 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.
Multitasking threads require less overhead than
multitasking processes. Processes are
heavyweight tasks that require their own
separate address spaces. Inter-process
communication is expensive and limited.
Context switching from one process to another
is also costly.
Threads, on the other hand, are lightweight.
They share the same address space and
cooperatively share the same heavyweight
process. Inter-thread communication is
inexpensive, and context switching from one
thread to the next is low cost.
The Java Thread Model:
Single-threaded systems use an approach
called an event loop with polling. In this
model, a single thread of control runs in an
infinite loop, polling a single event queue to
decide what to do next.
This single-threaded system waste the CPU time.
It can also result in one part of a program
dominating the system and preventing any other
events from being processed.
In general, in a single-threaded environment,
when a thread blocks (that is, suspends
execution) because it is waiting for some
resource, the entire program stops running.
The benefit of Java’s multithreading is that the
main loop/polling mechanism is eliminated. One
thread can pause, without stopping other parts of
our program.
When a thread blocks in a Java program, only the
single thread that is blocked pauses. All the
threads continue to run.
Threads exist in several states. A thread can
be running. It can be ready to run as soon as it
gets CPU time. A running thread can be
suspended, which temporarily suspends its
activity.
A suspended thread can then be resumed,
allowing it to pick up where it left off. A thread
can be blocked when waiting for a resource.
At any time, a thread can be terminated,
which halts its execution immediately.
Thread Priorities:
Thread priorities are integers that specify the
relative priority of one thread to another.
A thread’s priority is used to decide when to switch
from one running thread to the next. This is called a
“context switch”.
The rules that determine when a context switch
takes place are:
A thread can voluntarily relinquish control:
This is done by explicitly yielding, sleeping, or
blocking on pending I/O. All other threads are
examined, and the highest-priority thread that is
ready to run is given the CPU.
A thread can be preempted by a higher-priority
thread:
In this case, a lower-priority thread that does not
hold the processor is simply preempted by a higher-
priority thread. This is called preemptive
multitasking.
Synchronization:
Because multithreading introduces an
asynchronous behavior to our programs, there
must be a way to enforce synchronize when we
need it.
We must prevent one thread from writing data,
while another thread is in the middle of reading
it.
Java implements an elegant twist on an age-old
model of interposes synchronization called
monitor. In this way, a monitor can be used to
protect a shared asset from being manipulated
by more than one thread at a time.
Each object has its own implicit monitor, that is
automatically entered when one of the object’s
synchronized methods is called. Once a thread
is inside a synchronized method, no other
thread can call any other synchronized method
on the same object.
Messaging :
Java provides a clean, low-cost way for two or
more threads to talk to each other via calls to
predefined methods. Java’s messaging system
allows a thread to enter a synchronized
method on an object, and then wait there until
some other thread explicitly notifies it to come
out.
Thread Class and the Runnable Interface :