














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!















An Introduction
©Nauman Shamim@DCIS-PIEAS
Islamabad-Pakistan
Assignment
Threads and Multi Threading
Programs written in C\C++ [Some plat from support multi threaded program using system specific code libraries]
Example Animations in web browser Operating system
Threads and multi threading..
An audio clip downloading program One thread to download One thread to execute the downloaded code Java’s garbage collector One thread executes program One thread keep tracks of orphans Document printing software One thread renders the documents One thread starts printing the rendered documents One thread queues other jobs arriving mean while
Threads co-ordination with each other In case of audio clip downloading program the thread playing the audio clip should co -ordinate with downloading thread Difficult to keep track the execution of code while programming Threads synchronization
Multi Threading classes
The Thread class
Creates a thread whose name is specified with “name” variable
Creates a thread whose name is “Thread-” concatenated with a number e.g. Thread-1,Thread-
The heart of a thread When a thread execute the code in run( ) method executes independently
Used by the caller program Calls the run method and returns to the caller program The launched thread executes independently
Static method Can cause a thread to sleep for defined number of milliseconds A sleeping thread does nt contend for processor
Thread Life Cycle
When the thread object is created by not executed yet The thread is not doing any thing in this state
When start method is called Thread is waiting for its turn to execute
when the system assigns resources to the thread The thread’s run method is now executing its code
When the run method is completed or exception is raised A thread is disposed after it is dead
When the thread is issues some I/O request The thread comes back to ready state when I/O operation is completed
A suspended state , the thread is not doing anything
A wait state , becomes when notified
Creating Threads
Extend the Thread class Override the run( ) methods
public class MyThread extends Thread{
String name; public MyThread(){ }
public MyThread(String name){ this.name=name; }
public void run(){ try{ while(true){ Thread.sleep(2000); System.out.println("Thread + name ); } }catch(Exception e ) { e.printStackTrace(); } }
public static void main(String s[]){ MyThread t1=new MyThread("Thread1"); MyThread t2=new MyThread("Thread2"); MyThread t3=new MyThread("Thread3"); MyThread t4=new MyThread("Thread4"); t1.start(); t2.start(); t3.start(); t4.start(); System.out.println(“End main”); } //end main } //end of class MyThread
MyThread explained
public static void main(String s[]){ MyThread t1=new MyThread("Thread1"); MyThread t2=new MyThread("Thread2"); MyThread t3=new MyThread("Thread3"); MyThread t4=new MyThread("Thread4"); try{ t1.start();
t2.start();
t3.start();
t4.start();
Thread.sleep(3000); }catch(Exception e){e.printStackTrace();} System.out.println(“End main”);
} //end main } //end of class MyThread
run( ){ While(true){ System.out.println(“Thread = “+name); Thread.sleep(1000);} }
run( ){ While(true){ System.out.println(“Thread = “+name); Thread.sleep(1000);} }
run( ){ While(true){ System.out.println(“Thread = “+name); Thread.sleep(1000);} }
run( ){ While(true){ System.out.println(“Thread = “+name); Thread.sleep(1000);} }
Thread
Thread1: Thred
Thread Thread Thread Thread4:
Thread Thread Thread
Thread Thread Thread Thread End main:
End main Thread Thread Thread Thread
Threads Priorities
Thread.MIN_PRIORITY A constant 1 Thread.MAX_PRIORITY A constant 10
Thread.NOMR_PRIORITY A constant 5
On non-time sliced system Equal level threads execute turn by turn unless completed On-time sliced system Equal level threads executes only for a quantum either completed or not
Preemption can occur If a lower and a higher priority thread contend for processor higher priority thread will be given preference over lower priority thread Indefinite postponment A lower priority thread can fall to indefinite postponement by a higher priority thread Java Scheduler Controls, schedule and keep track of thread priority
Wait sleep notify and notifiyAll
Even lower priority thread in the queue can proceed
Will wait for ever unless notified by some thread The thread becomes ready when notified Executes its code when processor is assigned to it
Wait for a defined amount of time and then becomes ready
Cause the thread to sleep for specified amount time
Wait,sleep,notify
A producer consumer program
Producer class
try{ While(true){ int num; num=(int)Math.random()2000; sharedObj.setInt(num); Systme.out.println(“number produced “+num); Thread.sleep((int)Math.random()1000; } }catch(Exception e){e.printStackTrace();}