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

15 MultiThreading

Multithreading allows individual programs to appear to perform multiple tasks simultaneously by executing threads concurrently. A thread is an entity within a process that can execute independently. Multiprocessing allows multiple processes to run concurrently by time sharing the CPU. Java supports multithreading through the Thread class and Runnable interface. Threads have lifecycles and priorities that influence which threads the operating system schedules to run on the CPU.

Uploaded by

Anuja Sabnis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

15 MultiThreading

Multithreading allows individual programs to appear to perform multiple tasks simultaneously by executing threads concurrently. A thread is an entity within a process that can execute independently. Multiprocessing allows multiple processes to run concurrently by time sharing the CPU. Java supports multithreading through the Thread class and Runnable interface. Threads have lifecycles and priorities that influence which threads the operating system schedules to run on the CPU.

Uploaded by

Anuja Sabnis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

1

Multi Threading
2
Multiprocessing
Many tasks done simultaneously.
Program is a set of instructions and a A process
is a running instance of a program. .
Here the CPU is shared between different
processes.
E.g. A MS-Word application & MS-Excel
application windows opened simultaneously or in
general any two applications running
simultaneously is multiprocessing.


3
Multi processing
The OS provides you with a mechanism called
CONTEXT SWITCHING which enables it to
switch from process to process.
Here what happens is a programs entire context
(including variables, global variables, functions
etc..) are stored separately.
Therefore if one program is running, it is as if
other programs arent existing at all, because they
reside in their own contexts.
4
Multithreading
Different tasks within a task done simultaneously.
Extend the idea of multitasking by taking it one
level lower.
Individual programs appear to do multiple tasks at
the same time.
Each sub task is called Thread.
Think of each thread running in a separate
context - that is, each thread has its own CPU -
with registers, memory & own code.
5
Thread
A thread is an entity within a process.
You can say, A thread within a process
executes.
E.g.
Lets take a typical application like Word. There
are various independent processes going on,
like showing GUI, saving into file, typing in text.
In your java program main() is a thread &
garbage collector is another thread.

6
process & thread
Each process has a
complete set of its
own variables
It takes more
overhead to launch
new processes.
Inter-process
communication is
slower & restrictive
Threads may share
the same data.
It takes much less
overhead to create &
destroy threads
Communication
between threads is
easier.

7
Scheduling of process/thread
Scheduling is decided by OS.
Scheduling algorithms basically classified as:
Preemptive scheduling
OS interrupts programs without consulting them eg :
win 95, win NT
Non-preemptive scheduling
programs interrupted only when they are ready to yield
control. Eg : Sun Solaris, Win 3.1


8
Multithreading In Context With Java
Java supports multithreading.
But Java is platform dependent as far as
multithreading goes .
JVM is running on top of OS.
Thus, multithreading capability is totally
dependent on what scheduling algorithm is
followed by the underlying OS !!
Thus inconsistent behavior is observed in
multithreaded programs.
9
Multithreading In Context With Java
Java has a class Thread
Its a part of java.lang package

Constructors of Thread class -
public Thread()
public Thread(Runnable target)
public Thread(ThreadGroup group, String name)
public Thread(Runnable target, String name)
10
Life Cycle Of Thread
Methods of Thread class -
run() : must be overridden; code should be
added which executes in thread.
start() : brings the thread into ready to run
condition. When CPU is available for this
thread, invokes the run().
stop() : Forces the thread to stop executing. Its
a final method. (Deprecated)
sleep() : A static method, puts currently
executing thread to sleep for specified no. of
milliseconds.
11
born
ready
running
dead
sleeping waiting blocked suspended
start
Sleep intervals
expires
resume
quantum
expiration
dispatch
(assign a
processor)
complete stop
12
Multithreading
class PingPong extends Thread{
String word;
public PingPong(String w){
word = w;
}
public void run(){
try{
for( ;; ){
System.out .println(word);
Thread.sleep( 1000);
}catch(Exception e){System.out.println(e);}
}
13
Multithreading
public static void main(String args[])
{
Thread t1 = new PingPong(ping);
t1.start();
Thread t2 = new PingPong(pong);
t2.start();

}
}
14
Runnable Interface
Java can extend only one class.
What if we want multithreading support for a class
that is already derived from some other class?
Eg class MyFrame extends Frame
Solution ? Runnable interface.
Implementing this gives the ability to treat the new
class as a Runnable object.
15
Multithreading
class PingPong implements Runnable{
String word;
public PingPong(String w){
word = w;
}
public void run(){
try{
for( ;; ){
System.out .println(word);
Thread.sleep( 1000);
}
}catch(Exception e){System.out.println(e);}
}
16
Multithreading
public static void main(String args[])
{
Runnable ping = new PingPong(ping);
Runnable pong = new PingPong(pong);

new Thread(ping).start();
new Thread(pong).start();

}
}
17
Thread Priorities
Thread priorities are used by thread scheduler
To decide when each thread should be allowed
to run.
Threads of equal priority compete equally for
CPU.
Whenever the thread scheduler has a chance to
pick up a new thread, it generally picks up the
highest-priority thread that is currently runnable.

18
Setting thread priorities
To set threads priority -
final void setPriority(int level)
e.g. t1.setPriority(9);
Value of level should be in the range of
MAX_PRIORITY (10) & MIN_PRIORITY(1)
Default priority is NORM_PRIORITY(defined at 5)

19
Some methods of the Thread
class
join()
yield()
isAlive()
sleep()
currentThread()
interrupt()


20
join()
This method waits for a thread to terminate.
To do this, invoke join() on the thread object.
E.g.
Thread t=new ();
t.start();
t.join();
join() : waits forever for the thread to die.
join(long msecs): waits msecs time. If thread
doesn't die join() returns.

21
yield()
This methods causes currently executing thread
to yield.
If there are other runnable threads whose priority
is at least as high as this thread, they will be
scheduled next.
Again everything is dependent on the operating
systems choice of scheduling.
22
isAlive()
returns true if thread has started & has not yet
terminated.
A thread is dead if
It dies a natural death because the run()
method exits normally.
It dies abruptly because of an uncaught
exception.
23
currentThread()
static native Thread currentThread() - returns a reference
to current thread.
Can be used in run() method to check which thread is
currently executing
Eg : public void run(){
If (Thread.currentThread() == t1)
// do something

}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy