0% found this document useful (0 votes)
46 views33 pages

Multithreadinglec

Uploaded by

anuja_java
Copyright
© Attribution Non-Commercial (BY-NC)
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)
46 views33 pages

Multithreadinglec

Uploaded by

anuja_java
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 33

Multithreading

Multithreading
• Executing program with multiple threads in parallel.
• Multithreaded program contains two or more parts that can run
concurrently. Each part is called a thread and each part defines a
separate path of execution.
• Special form of multitasking

Multitasking
• Process-based
• Thread-based
Process
• A program in execution
• A computational unit in a system which uses the resources of
system when allocated by OS.
• A heavy weight entity
 Has own address space – variables and data structure in memory.
 May use file utility.
• Kernel-level controlled entity.
• Communicate via OS, files, networks.
• May contain multiple threads.
Thread
• Sequentially executed stream of instructions.
•A light weight entity
 Multiple threads of a process share common address space and data
of the process.
 Does not use file utility.
• process level controlled entity
Why Multithreading?
• Problem solving
 May have concurrent interacting components
 Can handle each component using separate thread
 Simplifies programming for problem
 Example:
WEB BROWSER

Web WEB BROWSER


Server
WEB BROWSER

Web server uses


threads to handle Multiple simultaneous
various web- web browser requests
browser
Contd..
• Better utilization of resources
 When a thread is delayed compute other thread
 If extra hardware is given, compute threads in parallal
 Reduce overall execution time
 Example:
Web
WEB BROWSER Server

WEB BROWSER
Web
WEB BROWSER Server
Multiple simultaneous
web browser requests Handled faster by multiple Web
Servers
Java Threads
• Java’s multithreading is built upon Thread class and its companion
interface Runnable.
• To create a thread
 Implement Runnable or
 Extend Thread
 Specify the work we want the thread to do
 Put the work in run( ) method

• The Thread class


public class Thread{
public Thread (Runnable R);
public Thread (Runnable R, String name);
--------------------------
public void run( );
}
More Thread class methods
public class Thread{
public String getName( ); //obtain a thread’s name
final boolean isAlive( ); //determine if the thread is still running
final void join( ); //wait for a thread to terminate
final void setName(String name); //change the internal name of thread
final void setPriority(int level); //set a thread’s priority
final int getPriority( ); //obtain the thread’s current priority
public static Thread currentThread( ); //obtain a reference to the thread in
// which it is called
public static void sleep(long milliseconds); //suspend a thread for a period
of time
The main thread
• All Java programs have one thread : main thread.
• It begins running immediately when a Java program starts up
• It is important for two reasons:
 It is the thread from which other “child” threads will be spawned.
 Often it must be the last thread to finish its execution because it
performs various shutdown actions.
• It is created automatically but can be controlled through a Thread
object
 Obtain a reference of it by calling currentThread( ) method.
class currentThreadDemo{

public static void main(String args[]){

Thread t=Thread.currentThread( );

System.out.println(“Current Thread: ” +t); //reference stored in local var. t

t.setName(“MyThread”); //change the name of the thread

System.out .println(“After name change” +t);

try{
An exception is thrown if
for(int i=5;i>0;i--) { System.out.println(i); some other thread want
to interrupt the sleeping
Thread.sleep(1000);} //pausiing for one second;
thread
}catch(InterruptedException e){System.out.println(“Main Thread interrupted”);}

}
}
Creating a Thread using Runnable
interface
• To implement Runnable a class need only implement a single
method run( ) which defines the code that constitutes the new
thread
• run( ) can call other methods, use classes and declare variables just
like main thread can.
• run( ) establishes an entry point for another concurrent thread of
execution within our program. This thread will end when run( )
returns.
• After implementing Runnable
 instantiate an object of type thread within the class
 Call the start method to start the new thread
 run( ) executes a call to run( )
class NewThread implements Runnable{
Thread t;
NewThread( ){
t = new Thread(this,”DemoThread”); //Constructor of thread class to
System.out.println(“Child thread:” +t); instantiate Thread class
t.start( ); }
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 Thread interrupted”);}
System.out.println(“Exiting Child Thread”);} }
class NewThreadDemo{
public static void main(String args[]){
new NewThread( ); //Create new thread
try{
for(int i=1;i<=5;i+) { System.out.println(“Main Thread:”+i);
Thread.sleep(1000);} //sleep 1000ms to ensure that main finishes last
}catch(InterruptedException e){System.out.println(“Main Thread interrupted”);}
System.out.println(“Exiting main thread”);} }
Creating a thread by extending
Thread class
• Create a new class that extends Thread
• Create an instance of that class
• The extending method must override the run method
• Must also call start( ) method to begin execution of new thread
class NewThread extendsThread{
//Thread t; no need to create this reference as Thread is already being inherited
NewThread( ){
Super(“DemoThread); //Constructor of super class
System.out.println(“Child thread:” +this); Thread is called
start( ); }
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 Thread interrupted”);}
System.out.println(“Exiting Child Thread”);} }
class NewThreadDemo{
public static void main(String args[]){
new NewThread( ); //Create new thread
try{
for(int i=1;i<=5;i+) { System.out.println(“Main Thread:”+i);
Thread.sleep(1000);} //sleep 1000ms to ensure that main finishes last
}catch(InterruptedException e){System.out.println(“Main Thread interrupted”);}
System.out.println(“Exiting main thread”);} }
Extending Thread class (not
recommended)
• The Thread class defines several methods that can be overridden
by a subclass.
• run( ) is the only one method that must be overridden, the same
method is required when we implement Runnable.
• In general the classes should be extended only when they are being
enhanced or modified in some way. So if no other Thread’s method
are overridden then implement Runnable.
Creating multiple threads
• Our program can spawn as many threads as it needs.
• Once started all threads share same CPU.
• Example:- Two threads are created with different names.
class NewThread implements Runnable{
Thread t; String name;
NewThread(String threadname ){ name=threadname;
t = new Thread(this,name); //Constructor of thread class to
System.out.println(“New thread:” +t); instantiate Thread class
t.start( ); }
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+ “Thread
System.out.println(name+
interrupted”);} “Exiting ”);} }
class MultiThreadDemo{
public static void main(String args[]){
new NewThread(“ONE” ); new NewThread(“SECOND” ); //Create new threads
try{
for(int i=1;i<=5;i+) { System.out.println(“Main Thread:”+i);
Thread.sleep(10000);}//sleep 1000ms to ensure that main finishes last
}catch(InterruptedException e){System.out.println(“Main Thread interrupted”);}
System.out.println(“Exiting main thread”);} }
Synchronization
• Websters: “To represent or arrange events to indicate coincidence
or coexistence.”
• Lewis : “To arrange events so that they occur in a specified order.”
• Serialized access to controlled resources.
• Thread Synchronization
 On shared memory : shared variables – semaphores
 On distributed memory:-
 within a task – semaphores
 across the task – by passing messages
Concurrent Execution
• More than one thread exists in system at once
• Can execute independently or in cooperation
• Asynchronous execution
 Threads generally independent
 Must occasionally communicate or synchronize
 Complex and difficult to manage such interactions
Atomic actions
• An action which must be started and completed with no possibility of
interruption.
 A machine instruction could need to be atomic. (not all are!)
 A line of C code could need to be atomic. (not all are)
 An entire database transaction could need to be atomic.

• All Multiprocessing machines provide at least one complex atomic


instruction, from which you can build anything.
• A section of code which you have forced to be atomic is a Critical
Section.
Critical Section
(BAD Programmer!)
T1 T2
Reader{ Writer{
------------------- -------------------
Lock(DISK) …………………
………………… …………………
………………… }
Unlock(DISK)
}

Shared Data
Critical Section
(Good Programmer!)
T1 T2
Reader{ Writer{
------------------- -------------------
Lock(DISK) Lock(DISK)
………………… …………………
………………… …………………
Unlock(DISK) Unlock(DISK)
} }

Shared Data
Lock Shared Data!
• Globals
• Shared data structures
• Static variables
(really just lexically scoped global variables)
Mutual Exclusion

• Problem of two threads accessing data simultaneously


 Data can be put in inconsistent state
 Context switch can occur at anytime, such as before a thread finishes
modifying value
 Such data must be accessed in mutually exclusive way
 Only one thread allowed access at one time
 Others must wait until resource is unlocked
 Must be managed such that waiting time not unreasonable
Mutexes

Thread 1 Thread2

item = create_and_fill_item(); mutex_lock( &m );


mutex_lock( &m ); this_item = list;
item->next = list; list = list_next;
list = item;
mutex_unlock(&m);
mutex_unlock(&m);
Java Terminology
• Key to synchronization is the concept of monitor(also called
semaphore)
• A monitor is an object that is used as an mutually exclusive
lock(mutex).
• Only one thread can own a monitor at a time.
• All the other threads attempting to enter the locked monitor will be
suspended until the first thread exits the monitor
• Synchronization can be achieved in two ways.
 Using synchronized methods
 Using synchronized statement (block)
Producer/Consumer relationship

• One thread creates data to store in shared object


• Second thread reads data from that object
• Large potential for data corruption if unsynchronized
Interthread Communication
• In the synchronized Producer-Consumer problem-
• suppose the producer has to wait until the consumer has consumed
the item before it generates more item
• In polling system, the Consumer would waste many CPU cycles
while it waited for Producer to produce.
• Once the Producer has finished, it would start polling, wasting more
CPU cycles waiting for the consumer to finish and so on
• To avoid polling, Java includes Iinterprocess communication
mechanism via
 wait( ): tells the calling thread to give up the monitor until some other
thread enters the same monitor and calls notify( )
 notify( ): wakes up the first thread that called wait( ) on the same object
 notify( ): wakes up all the threads that called wait( ) on the same
object. The highest priority thread will run first.
// an incorrect implementation of Producer Consumer

class Q{

int n;
Synchronized int get( ){
System.out.println(“Got: “ +n);
return n;
}

Synchronized void put(int n){


this.n = n;
System.out.println(“Put: “ +n);
}
}
Class Producer implements Runnable{
Q q;
Producer(Q q){
this.q = q;
new Thread(this, “Producer”).start( ); }
public void run( ){
int i = 0;
while(true) {
q.put(i++); } } }
class Consumer implements Runnable{
Q q;
Consumer(Q q){
this.q = q;
new Thread(this, “Consumer”).start( ); }
public void run( ){
while(true) {
q.get( ); } } }
class PC {
public static void main(String args[ ]) {
Q q = new Q( );
new Producer (q);
new Consumer (q);
System.out.println(“Press Control+c to stop”); } }
// a correct implementation of Producer Consumer
class Q{
int n;
boolean mutex = false;
synchronized void put(int n){
if(mutex)
try{
wait( );
}catch(InterruptedException e) { }
this.n = n;
mutex = true
System.out.println(“Put: “+n);
notify( ); } }
synchronized int get( ){
if(!mutex)
try{
wait( );
}catch(InterruptedException e){ }
System.out.println(“Got:” + n);
mutex = false;
notify( );
return n; }
Class Producer implements Runnable{
Q q;
Producer(Q q){
this.q = q;
new Thread(this, “Producer”).start( ); }
public void run( ){
int i = 0;
while(true) {
q.put(i++); } } }
class Consumer implements Runnable{
Q q;
Consumer(Q q){
this.q = q;
new Thread(this, “Consumer”).start( ); }
public void run( ){
while(true) {
q.get( ); } } }
class PC {
public static void main(String args[ ]) {
Q q = new Q( );
new Producer (q);
new Consumer (q);
System.out.println(“Press Control+c to stop”); } }
Synchronization Variables in
Shared Memory (Cross Process)

Process 1 Process 2
Synchronization
Variable S S
Shared Memory
S
Thread S

You might also like