SlideShare a Scribd company logo
1 of 63
3/14/2024 1
MULTITHREADED
PROGRAMMING
 A multithreaded program contains two or more
parts that can run concurrently. Each part of
such a program is called a thread, and each
thread defines a separate path of execution.
 Thus, multithreading is a specialized form of
multitasking.
 There are two distinct types of multitasking:
process-based and thread-based.
 A process is, in essence, a program that is
executing.
3/14/2024 2
3/14/2024 3
 In a thread-based multitasking environment, the
thread is the smallest unit of dispatchable code
 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
3/14/2024 4
 Threads, on the other hand, are lightweight
 They share the same address space and
cooperatively share the same heavyweight process
 Interthread communication is inexpensive, and
context switching from one thread to the next is low
cost
 Multithreading enables you to write very efficient
programs that make maximum use of the CPU,
because idle time can be kept to a minimum
3/14/2024 5
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
 Polling the mechanism, that selects a single event
from the event queue to choose what to do next. As
the event is selected, then event loop forwards the
control to the corresponding required event handler.
Nothing else can be happen, until the event handler
returns.
 Single-threaded systems wastes CPU time
3/14/2024 6
 In general, in a singled-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 your program.
 When a thread blocks in a Java program, only
the single thread that is blocked pauses; All
other threads continue to run.
3/14/2024 7
 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. Once
terminated, a thread cannot be resumed
3/14/2024 8
Thread Priorities
 Java assigns to each thread a priority that
determines how that thread should be treated with
respect to the others
 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
3/14/2024 9
 The rules that determine when a context
switch takes place are:
 A thread can voluntarily relinquish control:
This is done by sleeping, or blocking on
pending I/O.
 A thread can be preempted by a higher-
priority thread : In this case, a lower-priority
thread that does not yield the processor is
simply preempted—no matter what it is
doing—by a higher-priority thread.
3/14/2024 10
Synchronization
 Because multithreading introduces an
asynchronous behavior to your programs, there
must be a way for you to enforce synchronicity
when you need it.
 Java implements synchronization using
monitor (defined by C A R Hoare)
 Once a thread enters a monitor, all other
threads must wait until that thread exits the
monitor
 Once a thread is inside a synchronized method,
no other thread can call any other synchronized
method on the same object
3/14/2024 11
Messaging
 Java provides a clean, low-cost way for two
or more threads to talk to each other, via calls
to predefined methods that all objects have
 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
3/14/2024 12
The Thread Class and the Runnable
Interface
 Java’s multithreading system is built upon the
Thread class, its methods, and its companion
interface, Runnable
 Thread encapsulates a thread of execution.
 Thread instance is used to refer to the state
of running thread.
 To create a new thread, our program will
either extend Thread or implement the
Runnable interface
3/14/2024 13
 The Thread class defines several methods
that help manage threads
3/14/2024 14
The Main Thread
 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
 Main thread is important for following 2 reasons:
 It is the thread from which other “child” threads will
be spawned
 Often it must be the last thread to finish execution
because it performs various shutdown actions
3/14/2024 15
 Although the main thread is created automatically
when your program is started, it can be controlled
through a Thread object
 To do so, we must obtain a reference to it by calling
the method currentThread( ), which is a public
static member of Thread
static Thread currentThread( )
 This method returns a reference to the thread in
which it is called
3/14/2024 16
// Controlling the main Thread.
class CurrentThreadDemo {
public static void main(String args[]) {
Thread t = Thread.currentThread();
System.out.println("Current thread: " + t);
// change the name of the thread
t.setName("My Thread");
System.out.println("After name change: " + t);
try {
for(int n = 5; n > 0; n--) {
System.out.println(n);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main thread interrupted");
}
} }
3/14/2024 17
 The argument to sleep( ) specifies the delay period
in milliseconds
Output:
Current thread: Thread[main,5,main]
After name change: Thread[My Thread,5,main]
5
4
3
2
1
 t displays the name of the thread, its priority, and
the name of its group
3/14/2024 18
 By default, the name of the main thread is
main.
 Its priority is 5, which is the default value, and
main is also the name of the group of threads
to which this thread belongs
 A thread group is a data structure that
controls the state of a collection of threads as
a whole
 We can set the name of a thread by using
setName( )
 We can obtain the name of a thread by
calling getName( )
3/14/2024 19
Creating thread :
 We create a thread by instantiating an
object of type Thread
i) We can implement the Runnable interface
ii) We can extend the Thread class, itself
3/14/2024 20
Implementing Runnable
 We can construct a thread on any object that
implements Runnable
 To implement Runnable, a class need only
implement a single method called run( ), which is
declared like this:
public void run( )
 Inside run( ), you will define the code that
constitutes the new thread
3/14/2024 21
 Inside run( ), we will define the code that
constitutes the new thread
 run( ) establishes the entry point for another,
concurrent thread of execution within our
program
 After we create a class that implements
Runnable, we will instantiate an object of
type Thread from within that class
3/14/2024 22
 The constructor is:
Thread(Runnable threadOb, String threadName)
 threadOb is an instance of a class that implements
the Runnable interface; This defines where
execution of the thread will begin
 The name of the new thread is specified by
threadName
 After the new thread is created, it will not start
running until you call its start( ) method, which is
declared within Thread
 In essence, start( ) executes a call to run( )
void start( )
3/14/2024 23
// Create a second thread.
class NewThread implements Runnable {
Thread t;
NewThread() {
// Create a new, second thread
t = new Thread(this, "Demo Thread");
System.out.println("Child thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for the second thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
}
catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
3/14/2024 24
3/14/2024 25
class ThreadDemo {
public static void main(String args[]) {
new NewThread(); // create a new thread
try {
for(int i = 5; i > 0; i--) {
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
3/14/2024 26
Output:
Child thread: Thread[Demo Thread,5,main]
Main Thread: 5
Child Thread: 5
Child Thread: 4
Main Thread: 4
Child Thread: 3
Child Thread: 2
Main Thread: 3
Child Thread: 1
Exiting child thread.
Main Thread: 2
Main Thread: 1
Main thread exiting.
3/14/2024 27
Creating Multiple Threads
 So far, we have been using only two
threads: the main thread and one child
thread
 However, our program can spawn as
many threads as it needs
3/14/2024 28
// Create multiple threads.
class NewThread implements Runnable {
String name; // name of thread
Thread t;
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println(name + ": " + i);
3/14/2024 29
Thread.sleep(1000);
}
}
catch (InterruptedException e) {
System.out.println(name + "Interrupted");
}
System.out.println(name + " exiting.");
}
}
class MultiThreadDemo {
public static void main(String args[]) {
new NewThread("One"); // start threads
new NewThread("Two");
new NewThread("Three");
try {
// wait for other threads to end
Thread.sleep(10000);
}
catch (InterruptedException e) {
System.out.println("Main thread
Interrupted");
}
System.out.println("Main thread exiting.");
}
}
3/14/2024 30
3/14/2024 31
The output from this program is
shown here:
New thread: Thread[One,5,main]
New thread: Thread[Two,5,main]
New thread: Thread[Three,5,main]
One: 5
Two: 5
Three: 5
One: 4
Two: 4
Three: 4
One: 3
Three: 3
Two: 3
One: 2
Three: 2
Two: 2
One: 1
Three: 1
Two: 1
One exiting.
Two exiting.
Three exiting.
Main thread exiting.
3/14/2024 32
Extending Thread
 The second way to create a thread is to create a
new class that extends Thread, and then to create
an instance of that class
 The extending class must override the run( )
method, which is the entry point for the new thread
 It must also call start( ) to begin execution of the
new thread
3/14/2024 33
// Create a second thread by extending Thread
class NewThread extends Thread {
NewThread() {
// Create a new, second thread
super("Demo Thread");
System.out.println("Child thread: " + this);
start(); // Start the thread
}
// This is the entry point for the second thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
3/14/2024 34
}
catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
class ExtendThread {
public static void main(String args[]) {
new NewThread();
// create a new thread
try{
for(int i = 5; i > 0; i--) {
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
}
catch (InterruptedException e) {
System.out.println("Main thread
inturrupted.");
}
System.out.println("Main thread exiting.");
}
}
3/14/2024 35
3/14/2024 36
 Call to super( ) inside NewThread invokes
the following form of the Thread constructor:
public Thread(String threadName)
 Here, threadName specifies the name of the
thread
3/14/2024 37
 Extends Thread class vs Implements Runnable
Interface?
 Extending the Thread class will make your class unable to
extend other classes, because of the single inheritance
feature in JAVA. However, this will give you a simpler
code structure. If you implement Runnable, you can gain
better object-oriented design and consistency and also
avoid the single inheritance problems.
 If you just want to achieve basic functionality of a thread
you can simply implement Runnable interface and override
run() method. But if you want to do something serious with
thread object as it has other methods like suspend(),
resume(), etc which are not available in Runnable interface
3/14/2024 38
Using isAlive( ) and join( )
 How can one thread know when another thread has
ended?
 Two ways exist to determine whether a thread has
finished
 First, you can call isAlive( ) on the thread
 This method is defined by Thread, and its general
form is
final boolean isAlive( )
3/14/2024 39
 The isAlive( ) method returns true if the thread
upon which it is called is still running
 It returns false otherwise
 The method that you will more commonly use to wait
for a thread to finish is called join( ), shown here:
final void join( ) throws InterruptedException
 This method waits until the thread on which it is
called terminates
3/14/2024 40
// Using join() to wait for threads to finish.
class NewThread implements Runnable {
String name; // name of thread
Thread t;
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println(name + " interrupted.");
}
System.out.println(name + " exiting.");
} }
class DemoJoin {
public static void main(String args[]) {
NewThread ob1 = new NewThread("One");
NewThread ob2 = new NewThread("Two");
NewThread ob3 = new NewThread("Three");
System.out.println("Thread One is alive: "+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "+ ob3.t.isAlive());
3/14/2024 41
3/14/2024 42
// wait for threads to finish
try {
System.out.println("Waiting for threads to finish.");
ob1.t.join();
ob2.t.join();
ob3.t.join();
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Thread One is alive: "+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "+ ob3.t.isAlive());
System.out.println("Main thread exiting.");
} }
3/14/2024 43
Sample output from this program is shown here:
New thread: Thread[One,5,main]
New thread: Thread[Two,5,main]
New thread: Thread[Three,5,main]
Thread One is alive: true
Thread Two is alive: true
Thread Three is alive: true
Waiting for threads to finish.
One: 5
Two: 5
Three: 5
One: 4
Two: 4
Three: 4
One: 3
Two: 3
Three: 3
One: 2
Two: 2
Three: 2
One: 1
Two: 1
Three: 1
Two exiting.
Three exiting.
One exiting.
Thread One is alive: false
Thread Two is alive: false
Thread Three is alive: false
Main thread exiting.
3/14/2024 44
Thread Priorities
 In theory, higher-priority threads get more CPU time
than lower priority threads.
 In practice, the amount of CPU time that a thread gets
often depends on several factors besides its priority. A
higher-priority thread can also preempt a lower-priority
one
 For safety, threads that share the same priority should
yield control once in a while.
 This ensures that all threads have a chance to run
under a nonpreemptive operating system.
 In practice, even in nonpreemptive environments,
most threads still get a chance to run,
3/14/2024 45
because most threads inevitably encounter some
blocking situation, such as waiting for I/O. When
this happens, the blocked thread is suspended
and other threads can run.
 To set a thread’s priority, use the setPriority( )
method, which is a member of Thread. This is its
general form: final void setPriority(int level)
 level specifies the new priority setting for the
calling thread. The value of level must be within
the range MIN_PRIORITY and MAX_PRIORITY.
3/14/2024 46
 Currently, these values are 1 and 10,
respectively. To return a thread to default
priority, specify NORM_PRIORITY, which is
currently 5.
 To obtain the current priority setting by calling
the getPriority( ) method of Thread
final int getPriority( )
3/14/2024 47
3/14/2024 48
ThreadPriorityDemo.java: A program which shows
altering order of threads
by changing priority. */
class A extends Thread {
public void run() {
System.out.println(“Thread A started”);
for(int i = 1; i <= 4; i++) {
System.out.println(“t From ThreadA: i= ” + i);
}
System.out.println(“Exit from A”);
}
}
3/14/2024 49
class B extends Thread {
public void run() {
System.out.println(“Thread B started”);
for(int j = 1; j <= 4; j++) {
System.out.println(“t From ThreadB: j= ” +
j);
}
System.out.println(“Exit from B”);
}
}class C extends Thread {
public void run() {
System.out.println(“Thread C started”);
for(int k = 1; k <= 4; k++) {
System.out.println(“t From ThreadC: k= ” + k);
}
System.out.println(“Exit from C”);
}
}
3/14/2024 50
public class ThreadPriorityDemo {
public static void main(String args[]) {
A threadA = new A();
B threadB = new B();
C threadC = new C();
threadC.setPriority(Thread.MAX_PRIORITY);
threadB.setPriority(threadA.getPriority() + 1);
threadA.setPriority(Thread.MIN_PRIORITY);
System.out.println(“Started Thread A”);
threadA.start();
System.out.println(“Started Thread B”);
threadB.start();
System.out.println(“Started Thread C”);
threadC.start();
System.out.println(“End of main thread”);
}
}
3/14/2024 51
Output
Started Thread A
Started Thread B
Started Thread C
Thread B started
End of main thread
Thread C started
From ThreadC: k= 1
From ThreadC: k= 2
From ThreadC: k= 3
From ThreadC: k= 4
Exit from C
From ThreadB: j= 1
From ThreadB: j= 2
From ThreadB: j= 3
From ThreadB: j= 4
3/14/2024 52
Exit from B
Thread A started
From ThreadA: i= 1
From ThreadA: i= 2
From ThreadA: i= 3
From ThreadA: i= 4
Exit from A
The highest priority thread C is scheduled/executed first and the
thread A which has the lowest priority is scheduled last.
3/14/2024 53
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
(like semaphore)
3/14/2024 54
 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
3/14/2024 55
Using Synchronized Methods
// This program is not synchronized.
class Callme {
void call(String msg) { //prefix with synchronized
System.out.print("[" + msg);
try {
Thread.sleep(1000);
} catch(InterruptedException e) {
System.out.println("Interrupted");
}
System.out.println("]");
}
}
3/14/2024 56
class Caller implements Runnable {
String msg;
Callme target;
Thread t;
public Caller(Callme targ, String s) {
target = targ;
msg = s;
t = new Thread(this);
t.start();
}
public void run() {
target.call(msg);
}
}
3/14/2024 57
class Synch {
public static void main(String args[]) {
Callme target = new Callme();
Caller ob1 = new Caller(target, "Hello");
Caller ob2 = new Caller(target, "Synchronized");
Caller ob3 = new Caller(target, "World");
// wait for threads to end
try {
ob1.t.join();
ob2.t.join();
ob3.t.join();
} catch(InterruptedException e) {
System.out.println("Interrupted");
}
}
}
3/14/2024 58
 Here is the output produced by this program:
[Hello[Synchronized[World]
]
]
 In this program, nothing exists to stop all three
threads from calling the same method, on the
same object, at the same time
 This is known as a race condition, because the
three threads are racing each other to complete
the method
 In most situations, a race condition is more
subtle and less predictable, because you can’t
be sure when the context switch will occur
3/14/2024 59
 To fix the preceding program, you must serialize
access to call( )
 To do this, you simply need to precede call()’s
definition with the keyword synchronized
class Callme {
synchronized void call(String msg) {
...
 This prevents other threads from entering call( )
while another thread is using it
3/14/2024 60
 After synchronized has been added to call(), the
output of the program is as follows:
[Hello]
[Synchronized]
[World]
 Once a thread enters any synchronized method on
an instance, no other thread can enter any other
synchronized method on the same instance
 However, nonsynchronized methods on that
instance will continue to be callable
3/14/2024 61
 Interthread Communication
 Java includes an elegant interthread
communication mechanism via the wait(),
notify( ), and notifyAll( ) methods
 All three methods can be called only from within
a synchronized context
 wait( ) tells the calling thread to give up the
monitor and go to sleep until some other thread
enters the same monitor and calls notify( )
3/14/2024 62
 notify( ) wakes up the first thread that called
wait( ) on the same object
 notifyAll( ) wakes up all the threads that called
wait( ) on the same object; The highest priority
thread will run first.
 Although wait( ) normally waits until notify( ) or
notifyAll( ) is called, there is a possibility that in
very rare cases the waiting thread could be
awakened due to a spurious wakeup. In this
case, a waiting thread resumes without notify( )
or notifyAll( ) having been called.
Using Multithreading
 The key to utilizing multithreaded support effectively
is to think concurrently rather than serially. For
example, when you have two subsystems within a
program that can execute concurrently, make them
individual threads.
 With the careful use of multithreading, you can
create very efficient programs.
 however: If you create too many threads, you can
actually degrade the performance of your program
rather than enhance it. Remember, some overhead
is associated with context switching. If you create
too many threads, more CPU time will be spent
changing contexts than executing your program!
3/14/2024 63

More Related Content

Similar to 9.multi-threading latest(MB).ppt .

Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaMonika Mishra
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaKavitha713564
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaKavitha713564
 
Multithreading
MultithreadingMultithreading
Multithreadingsagsharma
 
Java Multithreading.pptx
Java Multithreading.pptxJava Multithreading.pptx
Java Multithreading.pptxRanjithaM32
 
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxArulmozhivarman8
 
Java Multithreading
Java MultithreadingJava Multithreading
Java MultithreadingRajkattamuri
 
Java multithreading
Java multithreadingJava multithreading
Java multithreadingMohammed625
 
Java - Concurrent programming - Thread's basics
Java - Concurrent programming - Thread's basicsJava - Concurrent programming - Thread's basics
Java - Concurrent programming - Thread's basicsRiccardo Cardin
 
Multithreading
MultithreadingMultithreading
MultithreadingF K
 

Similar to 9.multi-threading latest(MB).ppt . (20)

Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Thread model in java
Thread model in javaThread model in java
Thread model in java
 
advanced java ppt
advanced java pptadvanced java ppt
advanced java ppt
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Java Multithreading.pptx
Java Multithreading.pptxJava Multithreading.pptx
Java Multithreading.pptx
 
Java Threads
Java ThreadsJava Threads
Java Threads
 
Java thread life cycle
Java thread life cycleJava thread life cycle
Java thread life cycle
 
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptx
 
Lecture10
Lecture10Lecture10
Lecture10
 
Threading concepts
Threading conceptsThreading concepts
Threading concepts
 
Java unit 12
Java unit 12Java unit 12
Java unit 12
 
Threads
ThreadsThreads
Threads
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
 
Java multithreading
Java multithreadingJava multithreading
Java multithreading
 
Java - Concurrent programming - Thread's basics
Java - Concurrent programming - Thread's basicsJava - Concurrent programming - Thread's basics
Java - Concurrent programming - Thread's basics
 
multithreading
multithreadingmultithreading
multithreading
 
Multithreading
MultithreadingMultithreading
Multithreading
 

More from happycocoman

gas turbine cycles.pptx .
gas turbine cycles.pptx                    .gas turbine cycles.pptx                    .
gas turbine cycles.pptx .happycocoman
 
RECIPROCATING_AIR_COMPRESSOR.ppt .
RECIPROCATING_AIR_COMPRESSOR.ppt         .RECIPROCATING_AIR_COMPRESSOR.ppt         .
RECIPROCATING_AIR_COMPRESSOR.ppt .happycocoman
 
SURFACE TEXTURE 2022.pptx .
SURFACE TEXTURE 2022.pptx                  .SURFACE TEXTURE 2022.pptx                  .
SURFACE TEXTURE 2022.pptx .happycocoman
 
Numericals on Raciprocating air compressor.ppt
Numericals on  Raciprocating air compressor.pptNumericals on  Raciprocating air compressor.ppt
Numericals on Raciprocating air compressor.ppthappycocoman
 
Vapor_power cycles KM.pptx ..
Vapor_power cycles KM.pptx            ..Vapor_power cycles KM.pptx            ..
Vapor_power cycles KM.pptx ..happycocoman
 
Vapor power cycles by Anupama.pptx .
Vapor power cycles by Anupama.pptx     .Vapor power cycles by Anupama.pptx     .
Vapor power cycles by Anupama.pptx .happycocoman
 
Performance and Testing of Internal Combustion Engines.ppt
Performance and Testing of Internal Combustion Engines.pptPerformance and Testing of Internal Combustion Engines.ppt
Performance and Testing of Internal Combustion Engines.ppthappycocoman
 
ICenginesNumericals (1).pptx .
ICenginesNumericals (1).pptx             .ICenginesNumericals (1).pptx             .
ICenginesNumericals (1).pptx .happycocoman
 
Air standard cycles_PPT KM1.pptx .
Air standard cycles_PPT KM1.pptx          .Air standard cycles_PPT KM1.pptx          .
Air standard cycles_PPT KM1.pptx .happycocoman
 
Pressure Measurement ppt.pptx .
Pressure Measurement ppt.pptx               .Pressure Measurement ppt.pptx               .
Pressure Measurement ppt.pptx .happycocoman
 
Measurements & Measurement .Systems.pptx
Measurements & Measurement .Systems.pptxMeasurements & Measurement .Systems.pptx
Measurements & Measurement .Systems.pptxhappycocoman
 
Strain Measurement (NEW).pptx .
Strain Measurement (NEW).pptx               .Strain Measurement (NEW).pptx               .
Strain Measurement (NEW).pptx .happycocoman
 
Force and torque measurements.pptx .
Force and torque measurements.pptx      .Force and torque measurements.pptx      .
Force and torque measurements.pptx .happycocoman
 
Chapter 11 - SCREW THREADS sllides.pdf .
Chapter 11 - SCREW THREADS sllides.pdf       .Chapter 11 - SCREW THREADS sllides.pdf       .
Chapter 11 - SCREW THREADS sllides.pdf .happycocoman
 
Measurement of form errors.pptx .
Measurement of form errors.pptx            .Measurement of form errors.pptx            .
Measurement of form errors.pptx .happycocoman
 
9. Surface Texture - PPT.pdf .
9. Surface Texture - PPT.pdf               .9. Surface Texture - PPT.pdf               .
9. Surface Texture - PPT.pdf .happycocoman
 
10. Screw Threads - PPT.pdf .
10. Screw Threads - PPT.pdf                    .10. Screw Threads - PPT.pdf                    .
10. Screw Threads - PPT.pdf .happycocoman
 
Measurement of Form errors complete slides.pdf
Measurement of Form errors complete slides.pdfMeasurement of Form errors complete slides.pdf
Measurement of Form errors complete slides.pdfhappycocoman
 
Limits Fits and Tolerances ppt.pdf .
Limits Fits and Tolerances ppt.pdf     .Limits Fits and Tolerances ppt.pdf     .
Limits Fits and Tolerances ppt.pdf .happycocoman
 

More from happycocoman (20)

gas turbine cycles.pptx .
gas turbine cycles.pptx                    .gas turbine cycles.pptx                    .
gas turbine cycles.pptx .
 
RECIPROCATING_AIR_COMPRESSOR.ppt .
RECIPROCATING_AIR_COMPRESSOR.ppt         .RECIPROCATING_AIR_COMPRESSOR.ppt         .
RECIPROCATING_AIR_COMPRESSOR.ppt .
 
SURFACE TEXTURE 2022.pptx .
SURFACE TEXTURE 2022.pptx                  .SURFACE TEXTURE 2022.pptx                  .
SURFACE TEXTURE 2022.pptx .
 
Numericals on Raciprocating air compressor.ppt
Numericals on  Raciprocating air compressor.pptNumericals on  Raciprocating air compressor.ppt
Numericals on Raciprocating air compressor.ppt
 
Vapor_power cycles KM.pptx ..
Vapor_power cycles KM.pptx            ..Vapor_power cycles KM.pptx            ..
Vapor_power cycles KM.pptx ..
 
Vapor power cycles by Anupama.pptx .
Vapor power cycles by Anupama.pptx     .Vapor power cycles by Anupama.pptx     .
Vapor power cycles by Anupama.pptx .
 
Performance and Testing of Internal Combustion Engines.ppt
Performance and Testing of Internal Combustion Engines.pptPerformance and Testing of Internal Combustion Engines.ppt
Performance and Testing of Internal Combustion Engines.ppt
 
ICenginesNumericals (1).pptx .
ICenginesNumericals (1).pptx             .ICenginesNumericals (1).pptx             .
ICenginesNumericals (1).pptx .
 
Air standard cycles_PPT KM1.pptx .
Air standard cycles_PPT KM1.pptx          .Air standard cycles_PPT KM1.pptx          .
Air standard cycles_PPT KM1.pptx .
 
Pressure Measurement ppt.pptx .
Pressure Measurement ppt.pptx               .Pressure Measurement ppt.pptx               .
Pressure Measurement ppt.pptx .
 
Measurements & Measurement .Systems.pptx
Measurements & Measurement .Systems.pptxMeasurements & Measurement .Systems.pptx
Measurements & Measurement .Systems.pptx
 
Strain Measurement (NEW).pptx .
Strain Measurement (NEW).pptx               .Strain Measurement (NEW).pptx               .
Strain Measurement (NEW).pptx .
 
Force and torque measurements.pptx .
Force and torque measurements.pptx      .Force and torque measurements.pptx      .
Force and torque measurements.pptx .
 
FLOW(NEW).pptx .
FLOW(NEW).pptx                          .FLOW(NEW).pptx                          .
FLOW(NEW).pptx .
 
Chapter 11 - SCREW THREADS sllides.pdf .
Chapter 11 - SCREW THREADS sllides.pdf       .Chapter 11 - SCREW THREADS sllides.pdf       .
Chapter 11 - SCREW THREADS sllides.pdf .
 
Measurement of form errors.pptx .
Measurement of form errors.pptx            .Measurement of form errors.pptx            .
Measurement of form errors.pptx .
 
9. Surface Texture - PPT.pdf .
9. Surface Texture - PPT.pdf               .9. Surface Texture - PPT.pdf               .
9. Surface Texture - PPT.pdf .
 
10. Screw Threads - PPT.pdf .
10. Screw Threads - PPT.pdf                    .10. Screw Threads - PPT.pdf                    .
10. Screw Threads - PPT.pdf .
 
Measurement of Form errors complete slides.pdf
Measurement of Form errors complete slides.pdfMeasurement of Form errors complete slides.pdf
Measurement of Form errors complete slides.pdf
 
Limits Fits and Tolerances ppt.pdf .
Limits Fits and Tolerances ppt.pdf     .Limits Fits and Tolerances ppt.pdf     .
Limits Fits and Tolerances ppt.pdf .
 

Recently uploaded

Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 

Recently uploaded (20)

Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 

9.multi-threading latest(MB).ppt .

  • 2.  A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution.  Thus, multithreading is a specialized form of multitasking.  There are two distinct types of multitasking: process-based and thread-based.  A process is, in essence, a program that is executing. 3/14/2024 2
  • 3. 3/14/2024 3  In a thread-based multitasking environment, the thread is the smallest unit of dispatchable code  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
  • 4. 3/14/2024 4  Threads, on the other hand, are lightweight  They share the same address space and cooperatively share the same heavyweight process  Interthread communication is inexpensive, and context switching from one thread to the next is low cost  Multithreading enables you to write very efficient programs that make maximum use of the CPU, because idle time can be kept to a minimum
  • 5. 3/14/2024 5 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  Polling the mechanism, that selects a single event from the event queue to choose what to do next. As the event is selected, then event loop forwards the control to the corresponding required event handler. Nothing else can be happen, until the event handler returns.  Single-threaded systems wastes CPU time
  • 6. 3/14/2024 6  In general, in a singled-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 your program.  When a thread blocks in a Java program, only the single thread that is blocked pauses; All other threads continue to run.
  • 7. 3/14/2024 7  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. Once terminated, a thread cannot be resumed
  • 8. 3/14/2024 8 Thread Priorities  Java assigns to each thread a priority that determines how that thread should be treated with respect to the others  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
  • 9. 3/14/2024 9  The rules that determine when a context switch takes place are:  A thread can voluntarily relinquish control: This is done by sleeping, or blocking on pending I/O.  A thread can be preempted by a higher- priority thread : In this case, a lower-priority thread that does not yield the processor is simply preempted—no matter what it is doing—by a higher-priority thread.
  • 10. 3/14/2024 10 Synchronization  Because multithreading introduces an asynchronous behavior to your programs, there must be a way for you to enforce synchronicity when you need it.  Java implements synchronization using monitor (defined by C A R Hoare)  Once a thread enters a monitor, all other threads must wait until that thread exits the monitor  Once a thread is inside a synchronized method, no other thread can call any other synchronized method on the same object
  • 11. 3/14/2024 11 Messaging  Java provides a clean, low-cost way for two or more threads to talk to each other, via calls to predefined methods that all objects have  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
  • 12. 3/14/2024 12 The Thread Class and the Runnable Interface  Java’s multithreading system is built upon the Thread class, its methods, and its companion interface, Runnable  Thread encapsulates a thread of execution.  Thread instance is used to refer to the state of running thread.  To create a new thread, our program will either extend Thread or implement the Runnable interface
  • 13. 3/14/2024 13  The Thread class defines several methods that help manage threads
  • 14. 3/14/2024 14 The Main Thread  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  Main thread is important for following 2 reasons:  It is the thread from which other “child” threads will be spawned  Often it must be the last thread to finish execution because it performs various shutdown actions
  • 15. 3/14/2024 15  Although the main thread is created automatically when your program is started, it can be controlled through a Thread object  To do so, we must obtain a reference to it by calling the method currentThread( ), which is a public static member of Thread static Thread currentThread( )  This method returns a reference to the thread in which it is called
  • 16. 3/14/2024 16 // Controlling the main Thread. class CurrentThreadDemo { public static void main(String args[]) { Thread t = Thread.currentThread(); System.out.println("Current thread: " + t); // change the name of the thread t.setName("My Thread"); System.out.println("After name change: " + t); try { for(int n = 5; n > 0; n--) { System.out.println(n); Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println("Main thread interrupted"); } } }
  • 17. 3/14/2024 17  The argument to sleep( ) specifies the delay period in milliseconds Output: Current thread: Thread[main,5,main] After name change: Thread[My Thread,5,main] 5 4 3 2 1  t displays the name of the thread, its priority, and the name of its group
  • 18. 3/14/2024 18  By default, the name of the main thread is main.  Its priority is 5, which is the default value, and main is also the name of the group of threads to which this thread belongs  A thread group is a data structure that controls the state of a collection of threads as a whole  We can set the name of a thread by using setName( )  We can obtain the name of a thread by calling getName( )
  • 19. 3/14/2024 19 Creating thread :  We create a thread by instantiating an object of type Thread i) We can implement the Runnable interface ii) We can extend the Thread class, itself
  • 20. 3/14/2024 20 Implementing Runnable  We can construct a thread on any object that implements Runnable  To implement Runnable, a class need only implement a single method called run( ), which is declared like this: public void run( )  Inside run( ), you will define the code that constitutes the new thread
  • 21. 3/14/2024 21  Inside run( ), we will define the code that constitutes the new thread  run( ) establishes the entry point for another, concurrent thread of execution within our program  After we create a class that implements Runnable, we will instantiate an object of type Thread from within that class
  • 22. 3/14/2024 22  The constructor is: Thread(Runnable threadOb, String threadName)  threadOb is an instance of a class that implements the Runnable interface; This defines where execution of the thread will begin  The name of the new thread is specified by threadName  After the new thread is created, it will not start running until you call its start( ) method, which is declared within Thread  In essence, start( ) executes a call to run( ) void start( )
  • 23. 3/14/2024 23 // Create a second thread. class NewThread implements Runnable { Thread t; NewThread() { // Create a new, second thread t = new Thread(this, "Demo Thread"); System.out.println("Child thread: " + t); t.start(); // Start the thread } // This is the entry point for the second thread.
  • 24. public void run() { try { for(int i = 5; i > 0; i--) { System.out.println("Child Thread: " + i); Thread.sleep(500); } } catch (InterruptedException e) { System.out.println("Child interrupted."); } System.out.println("Exiting child thread."); } } 3/14/2024 24
  • 25. 3/14/2024 25 class ThreadDemo { public static void main(String args[]) { new NewThread(); // create a new thread try { for(int i = 5; i > 0; i--) { System.out.println("Main Thread: " + i); Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println("Main thread interrupted."); } System.out.println("Main thread exiting."); } }
  • 26. 3/14/2024 26 Output: Child thread: Thread[Demo Thread,5,main] Main Thread: 5 Child Thread: 5 Child Thread: 4 Main Thread: 4 Child Thread: 3 Child Thread: 2 Main Thread: 3 Child Thread: 1 Exiting child thread. Main Thread: 2 Main Thread: 1 Main thread exiting.
  • 27. 3/14/2024 27 Creating Multiple Threads  So far, we have been using only two threads: the main thread and one child thread  However, our program can spawn as many threads as it needs
  • 28. 3/14/2024 28 // Create multiple threads. class NewThread implements Runnable { String name; // name of thread Thread t; NewThread(String threadname) { name = threadname; t = new Thread(this, name); System.out.println("New thread: " + t); t.start(); // Start the thread } // This is the entry point for thread. public void run() { try { for(int i = 5; i > 0; i--) { System.out.println(name + ": " + i);
  • 29. 3/14/2024 29 Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println(name + "Interrupted"); } System.out.println(name + " exiting."); } } class MultiThreadDemo { public static void main(String args[]) { new NewThread("One"); // start threads new NewThread("Two"); new NewThread("Three");
  • 30. try { // wait for other threads to end Thread.sleep(10000); } catch (InterruptedException e) { System.out.println("Main thread Interrupted"); } System.out.println("Main thread exiting."); } } 3/14/2024 30
  • 31. 3/14/2024 31 The output from this program is shown here: New thread: Thread[One,5,main] New thread: Thread[Two,5,main] New thread: Thread[Three,5,main] One: 5 Two: 5 Three: 5 One: 4 Two: 4 Three: 4 One: 3 Three: 3 Two: 3 One: 2 Three: 2 Two: 2 One: 1 Three: 1 Two: 1 One exiting. Two exiting. Three exiting. Main thread exiting.
  • 32. 3/14/2024 32 Extending Thread  The second way to create a thread is to create a new class that extends Thread, and then to create an instance of that class  The extending class must override the run( ) method, which is the entry point for the new thread  It must also call start( ) to begin execution of the new thread
  • 33. 3/14/2024 33 // Create a second thread by extending Thread class NewThread extends Thread { NewThread() { // Create a new, second thread super("Demo Thread"); System.out.println("Child thread: " + this); start(); // Start the thread } // This is the entry point for the second thread. public void run() { try { for(int i = 5; i > 0; i--) { System.out.println("Child Thread: " + i); Thread.sleep(500); }
  • 34. 3/14/2024 34 } catch (InterruptedException e) { System.out.println("Child interrupted."); } System.out.println("Exiting child thread."); } } class ExtendThread { public static void main(String args[]) { new NewThread(); // create a new thread try{ for(int i = 5; i > 0; i--) { System.out.println("Main Thread: " + i);
  • 35. Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println("Main thread inturrupted."); } System.out.println("Main thread exiting."); } } 3/14/2024 35
  • 36. 3/14/2024 36  Call to super( ) inside NewThread invokes the following form of the Thread constructor: public Thread(String threadName)  Here, threadName specifies the name of the thread
  • 37. 3/14/2024 37  Extends Thread class vs Implements Runnable Interface?  Extending the Thread class will make your class unable to extend other classes, because of the single inheritance feature in JAVA. However, this will give you a simpler code structure. If you implement Runnable, you can gain better object-oriented design and consistency and also avoid the single inheritance problems.  If you just want to achieve basic functionality of a thread you can simply implement Runnable interface and override run() method. But if you want to do something serious with thread object as it has other methods like suspend(), resume(), etc which are not available in Runnable interface
  • 38. 3/14/2024 38 Using isAlive( ) and join( )  How can one thread know when another thread has ended?  Two ways exist to determine whether a thread has finished  First, you can call isAlive( ) on the thread  This method is defined by Thread, and its general form is final boolean isAlive( )
  • 39. 3/14/2024 39  The isAlive( ) method returns true if the thread upon which it is called is still running  It returns false otherwise  The method that you will more commonly use to wait for a thread to finish is called join( ), shown here: final void join( ) throws InterruptedException  This method waits until the thread on which it is called terminates
  • 40. 3/14/2024 40 // Using join() to wait for threads to finish. class NewThread implements Runnable { String name; // name of thread Thread t; NewThread(String threadname) { name = threadname; t = new Thread(this, name); System.out.println("New thread: " + t); t.start(); // Start the thread } // This is the entry point for thread. public void run() { try { for(int i = 5; i > 0; i--) { System.out.println(name + ": " + i); Thread.sleep(1000); }
  • 41. } catch (InterruptedException e) { System.out.println(name + " interrupted."); } System.out.println(name + " exiting."); } } class DemoJoin { public static void main(String args[]) { NewThread ob1 = new NewThread("One"); NewThread ob2 = new NewThread("Two"); NewThread ob3 = new NewThread("Three"); System.out.println("Thread One is alive: "+ ob1.t.isAlive()); System.out.println("Thread Two is alive: "+ ob2.t.isAlive()); System.out.println("Thread Three is alive: "+ ob3.t.isAlive()); 3/14/2024 41
  • 42. 3/14/2024 42 // wait for threads to finish try { System.out.println("Waiting for threads to finish."); ob1.t.join(); ob2.t.join(); ob3.t.join(); } catch (InterruptedException e) { System.out.println("Main thread Interrupted"); } System.out.println("Thread One is alive: "+ ob1.t.isAlive()); System.out.println("Thread Two is alive: "+ ob2.t.isAlive()); System.out.println("Thread Three is alive: "+ ob3.t.isAlive()); System.out.println("Main thread exiting."); } }
  • 43. 3/14/2024 43 Sample output from this program is shown here: New thread: Thread[One,5,main] New thread: Thread[Two,5,main] New thread: Thread[Three,5,main] Thread One is alive: true Thread Two is alive: true Thread Three is alive: true Waiting for threads to finish. One: 5 Two: 5 Three: 5 One: 4 Two: 4 Three: 4 One: 3
  • 44. Two: 3 Three: 3 One: 2 Two: 2 Three: 2 One: 1 Two: 1 Three: 1 Two exiting. Three exiting. One exiting. Thread One is alive: false Thread Two is alive: false Thread Three is alive: false Main thread exiting. 3/14/2024 44
  • 45. Thread Priorities  In theory, higher-priority threads get more CPU time than lower priority threads.  In practice, the amount of CPU time that a thread gets often depends on several factors besides its priority. A higher-priority thread can also preempt a lower-priority one  For safety, threads that share the same priority should yield control once in a while.  This ensures that all threads have a chance to run under a nonpreemptive operating system.  In practice, even in nonpreemptive environments, most threads still get a chance to run, 3/14/2024 45
  • 46. because most threads inevitably encounter some blocking situation, such as waiting for I/O. When this happens, the blocked thread is suspended and other threads can run.  To set a thread’s priority, use the setPriority( ) method, which is a member of Thread. This is its general form: final void setPriority(int level)  level specifies the new priority setting for the calling thread. The value of level must be within the range MIN_PRIORITY and MAX_PRIORITY. 3/14/2024 46
  • 47.  Currently, these values are 1 and 10, respectively. To return a thread to default priority, specify NORM_PRIORITY, which is currently 5.  To obtain the current priority setting by calling the getPriority( ) method of Thread final int getPriority( ) 3/14/2024 47
  • 48. 3/14/2024 48 ThreadPriorityDemo.java: A program which shows altering order of threads by changing priority. */ class A extends Thread { public void run() { System.out.println(“Thread A started”); for(int i = 1; i <= 4; i++) { System.out.println(“t From ThreadA: i= ” + i); } System.out.println(“Exit from A”); } }
  • 49. 3/14/2024 49 class B extends Thread { public void run() { System.out.println(“Thread B started”); for(int j = 1; j <= 4; j++) { System.out.println(“t From ThreadB: j= ” + j); } System.out.println(“Exit from B”); } }class C extends Thread { public void run() { System.out.println(“Thread C started”); for(int k = 1; k <= 4; k++) { System.out.println(“t From ThreadC: k= ” + k); } System.out.println(“Exit from C”); } }
  • 50. 3/14/2024 50 public class ThreadPriorityDemo { public static void main(String args[]) { A threadA = new A(); B threadB = new B(); C threadC = new C(); threadC.setPriority(Thread.MAX_PRIORITY); threadB.setPriority(threadA.getPriority() + 1); threadA.setPriority(Thread.MIN_PRIORITY); System.out.println(“Started Thread A”); threadA.start(); System.out.println(“Started Thread B”); threadB.start(); System.out.println(“Started Thread C”); threadC.start(); System.out.println(“End of main thread”); } }
  • 51. 3/14/2024 51 Output Started Thread A Started Thread B Started Thread C Thread B started End of main thread Thread C started From ThreadC: k= 1 From ThreadC: k= 2 From ThreadC: k= 3 From ThreadC: k= 4 Exit from C From ThreadB: j= 1 From ThreadB: j= 2 From ThreadB: j= 3 From ThreadB: j= 4
  • 52. 3/14/2024 52 Exit from B Thread A started From ThreadA: i= 1 From ThreadA: i= 2 From ThreadA: i= 3 From ThreadA: i= 4 Exit from A The highest priority thread C is scheduled/executed first and the thread A which has the lowest priority is scheduled last.
  • 53. 3/14/2024 53 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 (like semaphore)
  • 54. 3/14/2024 54  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
  • 55. 3/14/2024 55 Using Synchronized Methods // This program is not synchronized. class Callme { void call(String msg) { //prefix with synchronized System.out.print("[" + msg); try { Thread.sleep(1000); } catch(InterruptedException e) { System.out.println("Interrupted"); } System.out.println("]"); } }
  • 56. 3/14/2024 56 class Caller implements Runnable { String msg; Callme target; Thread t; public Caller(Callme targ, String s) { target = targ; msg = s; t = new Thread(this); t.start(); } public void run() { target.call(msg); } }
  • 57. 3/14/2024 57 class Synch { public static void main(String args[]) { Callme target = new Callme(); Caller ob1 = new Caller(target, "Hello"); Caller ob2 = new Caller(target, "Synchronized"); Caller ob3 = new Caller(target, "World"); // wait for threads to end try { ob1.t.join(); ob2.t.join(); ob3.t.join(); } catch(InterruptedException e) { System.out.println("Interrupted"); } } }
  • 58. 3/14/2024 58  Here is the output produced by this program: [Hello[Synchronized[World] ] ]  In this program, nothing exists to stop all three threads from calling the same method, on the same object, at the same time  This is known as a race condition, because the three threads are racing each other to complete the method  In most situations, a race condition is more subtle and less predictable, because you can’t be sure when the context switch will occur
  • 59. 3/14/2024 59  To fix the preceding program, you must serialize access to call( )  To do this, you simply need to precede call()’s definition with the keyword synchronized class Callme { synchronized void call(String msg) { ...  This prevents other threads from entering call( ) while another thread is using it
  • 60. 3/14/2024 60  After synchronized has been added to call(), the output of the program is as follows: [Hello] [Synchronized] [World]  Once a thread enters any synchronized method on an instance, no other thread can enter any other synchronized method on the same instance  However, nonsynchronized methods on that instance will continue to be callable
  • 61. 3/14/2024 61  Interthread Communication  Java includes an elegant interthread communication mechanism via the wait(), notify( ), and notifyAll( ) methods  All three methods can be called only from within a synchronized context  wait( ) tells the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify( )
  • 62. 3/14/2024 62  notify( ) wakes up the first thread that called wait( ) on the same object  notifyAll( ) wakes up all the threads that called wait( ) on the same object; The highest priority thread will run first.  Although wait( ) normally waits until notify( ) or notifyAll( ) is called, there is a possibility that in very rare cases the waiting thread could be awakened due to a spurious wakeup. In this case, a waiting thread resumes without notify( ) or notifyAll( ) having been called.
  • 63. Using Multithreading  The key to utilizing multithreaded support effectively is to think concurrently rather than serially. For example, when you have two subsystems within a program that can execute concurrently, make them individual threads.  With the careful use of multithreading, you can create very efficient programs.  however: If you create too many threads, you can actually degrade the performance of your program rather than enhance it. Remember, some overhead is associated with context switching. If you create too many threads, more CPU time will be spent changing contexts than executing your program! 3/14/2024 63

Editor's Notes

  1. The Runnable interface marks an object that can be run as a thread. It has only one method, run, that contains the code that's executed in the thread. (The Thread class itself implements Runnable, which is why the Thread class has a run method.)
  2. Subtle:precise
  3. Spurious wakeup describes a complication in the use of condition variables as provided by certain multithreading APIs such as POSIX Threads and the Windows API.