Multi Threading (1)
Multi Threading (1)
Multithread vs Multiprocessing
Multitasking is a process of executing multiple tasks simultaneously. We use
multitasking to utilize the CPU. Multitasking can be achieved in two ways:
• Process-based Multitasking (Multiprocessing)
• Thread-based Multitasking (Multithreading)
Method Meaning
• Java multi-threading provides two ways to find whether a thread has finished
• A thread is alive if it has been started and has not yet died. This method is
used to find out if a thread has actually been started and has yet not
terminated.
2.Join()
The method more commonly used to wait for a thread to finish is called join()
final void join( ) throws InterruptedException
This method waits until the thread on which it is called terminates
Additional forms of join( ) allow you to specify a maximum amount of time
that you want to wait for the specified thread to terminate.
Here is an improved version of the preceding example that uses join( ) to
ensure that the
main thread is the last to stop
try
{
/*System.out.println("Child Thread Status " +
A.t.isAlive());
A.t.join();
System.out.println("Child Thread Status " +
A.t.isAlive());*/
Synchronization
• When two or more threads need access to a shared resource, they need some
way to ensure that the resource will be used by only one thread at a time. The
process by which this is achieved is called synchronization.
• Key to synchronization is the concept of the monitor (also called a
semaphore). A monitor is an object that is used as a mutually exclusive lock, or
mutex. Only one thread can own a monitor at a given time. When a thread
acquires a lock, it is said to have entered the monitor.
• All other threads attempting to enter the locked monitor will be suspended
until the first thread exits the monitor. These other threads are said to be
waiting for the monitor. A thread that owns a monitor can re-enter the same
monitor if it so desires.
• Instead, to synchronize threads, your programs need to utilize operating
system primitives. Fortunately, because Java implements synchronization
through language elements, most of the complexity associated with
synchronization has been eliminated.
Using Synchronized Methods
• Synchronization is easy in Java, because all objects
have their own implicit monitor associated with them.
• To enter an object’s monitor, just call a method that
has been modified with the synchronized keyword.
• While a thread is inside a synchronized method, all
other threads that try to call it on the same instance
have to wait.
• To exit the monitor and relinquish control of the object
to the next waiting thread, the owner of the monitor
simply returns from the synchronized method.
The synchronized Statement
• Calls to the methods defined by this class inside a
synchronized block.
• This is the general form of the synchronized statement:
synchronized(object) {
// statements to be synchronized
}
• A synchronized block ensures that a call to a method
that is a member of object occurs only after the current
thread has successfully entered object’s monitor.
Inter-thread Communication
• The threads can communicate with each other through wait() notify() and notifyAll() methods
in Java.
• These are final methods defined in the Object class and can be called only from within
a synchronized context.
• The wait() method causes the current thread to wait until another thread invokes
the notify() or notifyAll() methods for that object.
• The notify() method wakes up a single thread that is waiting on that object’s monitor.
• The notifyAll() method wakes up all threads that are waiting on that object’s monitor.
• A thread waits on an object’s monitor by calling one of the wait() method. These methods can
throw IllegalMonitorStateException if the current thread is not the owner of the object’s
monitor.
public final void wait() throws InterruptedException
public final void notify()
public final void notifyAll()
• Although wait( ) normally waits until notify( ) or notifyAll( )