













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An introduction to java multi-threading, covering the concept of threads, thread life cycle, and synchronization. It explains the difference between single and multi-threaded programs, the importance of thread priorities, and the use of wait, sleep, notify, and notifyall methods. The document also includes examples of producer-consumer programs and synchronized methods.
Typology: Slides
1 / 21
This page cannot be seen from the preview
Don't miss anything!
An
Introduction
Outline
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 in Java
Only by a higher priority thread, i.e. the processor is given to the higher priority thread The low priority thread (which is preempted) must have to wait
Each thread is given a small amount of time on processor Other threads wait for their turn
Higher priority thread By an equal priority thread
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 class..cont..
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 Expalined
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
Thread Synchronization
An ATM machine operation Only one machines is allowed to access the account balance of a user after which other machines can perform operations on that account
When two or more threads use such a shared object the sharing must be synchronized
The shared object is locked for the duration of operations by a thread
Used to declare a shared object as synchronized
Only one will execute at a time
Fall in a wait state that exits when notified
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();}
Consumer class
try{
While(true){
int num;
Num=sharedObj.getInt(num);
Systme.out.println(“Number consumed “+num);
Thread.sleep((int)Math.random()*1000;
}
}catch(Exception e){e.printStackTrace();}