VIPS OOPS Unit 3 Multithreading
VIPS OOPS Unit 3 Multithreading
• A thread goes through various stages of its life cycle. Example, first of all, a
thread is born, started its tasks, run a sequence of tasks concurrently, and then
dies. Here is the diagram of the various stages of a life cycle.
• New Thread: A new thread begins its life cycle in the new state. The process
remains in this condition until the program starts the thread.
• Runnable: As soon as the new thread starts, the thread status becomes Runnable.
At this stage, a thread is considered to execute its function or working.
• Not Runnable: A Runnable thread when entered the time of waiting for the state
for a specific interval of time. That time, the thread is not in Runnable condition.
• Dead / Terminated: The Runnable thread enters the end stage when it completes
its tasks.
Life Cycle of a Thread in Java
Life Cycle of a Thread in Java
Life Cycle of a Thread in Java
New: When instance of thread is created using new operator it is in new state,
but the start() method has not been invoked on the thread yet, thread is not
eligible to run yet.
• When a Java program starts up, one thread begins running immediately.
This is usually called the main thread of your program, because it is
the one that is executed when your program begins. The main thread is
important for two reasons:
• It is the thread from which other “child” threads will be born.
• Often, it must be the last thread to finish execution because it performs
various shutdown actions.
Although the main thread is created automatically when your program is
started, it can be controlled through a Thread object.
The Main Thread Example
void start( );
Create a Thread by Extending a Thread
Class // Create a second thread by extending Thread
System.out.println("Child interrupted.");
class NewThread extends Thread { } OUTPUT:
System.out.println("Exiting child thread."); Child thread:
NewThread() {
}}
// Create a new, second thread Thread[DemoThread,5,main]
class ExtendThread {
public static void main(String args[]) {
Main Thread: 5
super("Demo Thread");
new NewThread(); // create a new thread Child Thread: 5
System.out.println("Child thread: " + this);
try { Child Thread: 4
start(); // Start the thread Child Thread: 3
for(int i = 5; i > 0; i--) {
} System.out.println("Main Thread: " + i); Main Thread: 4
// This is the entry point for the second thread. Thread.sleep(1000); Child Thread: 2
} Main Thread: 3
public void run() {
} catch (InterruptedException e) { Child Thread: 1
try { System.out.println("Main thread interrupted.");
Exiting child thread.
for(int i = 5; i > 0; i--) { }
System.out.println("Main thread exiting.");
Main Thread: 2
System.out.println("Child Thread: " + i); Main Thread: 1
}}
Thread.sleep(500); Main thread exiting.
} BUILD SUCCESSFUL (total time: 5
} catch (InterruptedException e) { seconds)
Create a Thread by Extending a Thread
Class
The child Thread is created by instantiating an object of
NewThread, which is derived from Thread.
Notice the call to super( ) inside NewThread. This invokes the
following form of the
Thread constructor:
public Thread(String threadName)
Here, threadName specifies the name of the thread.
Multithreading catch (InterruptedException e) {
/ Create multiple threads. System.out.println(name + "Interrupted"); OUTPUT:
class NewThread implements Runnable { The output from this program is shown here:
}
New thread: Thread[One,5,main]
String name; // name of thread System.out.println(name + " exiting."); New thread: Thread[Two,5,main]
Thread t; New thread: Thread[Three,5,main]
}}
NewThread(String threadname) { One: 5
System.out.println(name + " exiting.");} } Two: 5
name = threadname;
class MultiThreadDemo { Three: 5
t = new Thread(this, name); One: 4
public static void main(String args[]) {
System.out.println("New thread: " + t); Two: 4
new NewThread("One"); // start threads Three: 4
t.start(); // Start the thread
new NewThread("Two"); One: 3
}
Three: 3
// This is the entry point for thread. new NewThread("Three");
Two: 3
public void run() { try { One: 2
try { // wait for other threads to end Three: 2
Two: 2
for(int i = 5; i > 0; i--) { Thread.sleep(10000); One: 1
System.out.println(name + ": " + i); } catch (InterruptedException e) { Three: 1
Thread.sleep(1000); System.out.println("Main thread Interrupted");
Two: 1
} One exiting.
} Two exiting.
} System.out.println("Main thread exiting."); Three exiting.
Main thread exiting.
}}
Thread Priorities
Java assigns each thread a priority that concludes that how a thread will
be treated concerning others. Thread priorities are integers that specify
the relative priority of one thread with another. Thread priorities are
used for deciding when to switch from one running thread to another. It
is called a context switch.
Thread Priorities
• Only one thread can own a monitor at a This is the general form of the
given time. When a thread acquires a synchronized statement:
lock, it is said to have entered the
monitor. synchronized(object) {
• All other threads attempting to enter the // statements to be synchronized
locked monitor will be suspended until
the first thread exits the monitor. }
• These other threads are said to be
waiting for the monitor.
• While a thread is inside a synchronized
method, all other threads that try to call
it (or any other synchronized method)
on the same instance have to wait.
Synchronization
This example is not synchronized.
• These method are implemented as final in Object. All three method can be called
only from within a synchronized context.
• wait() tells calling thread to give up monitor and go to sleep until some other
thread enters the same monitor and call notify.
• notifyAll() wakes up all the thread that called wait() on same object.
Interthread Communication
wait() sleep()
called from synchronised no such requirement
block
monitor is released monitor is not released
awake when notify() or not awake when notify() or
notifyAll() method is called. notifyAll() method is called
not a static method static method
wait() is generaly used on sleep() method is simply used to
condition put your thread on sleep.