MultithreadingLecture2
MultithreadingLecture2
TECHNOLOGY
Threads with higher priority are more important to a program and should be
allocated processor time before lower-priority threads. However, thread priorities
cannot guarantee the order in which threads execute and are very much platform
dependent.
Thread Priorities and Thread
Scheduling
Java thread priority
Priority in range 1-10
Time-slicing
Each thread assigned time on the processor (called a quantum)
Keeps highest priority threads running
5
Thread Priorities & Scheduling
Ready threads
Thread.MAX_PRIORITY Priority 10 A B
Priority 9 C
Priority 8
Priority 7 D E F
Priority 6 G
Priorities Thread.NORM_PRIORITY
Priority 5 H I
and Priority 4
Scheduli
Priority 3
Priority 2 J K
ng Thread.MIN_PRIORITY Priority 1
6
Thread Priorities
• Every thread in java has some priority
• It may be default priority generated by JVM
Or
• It may be customized priority provided explicitly by programmer
Q Who is going to use thread priorities and when thread priorities will be required
• Suppose In a processor multiple threads are waiting to get the chances for processor , so In
that case thread scheduler will give chance to that thread which is having highest priority
• The thread which is having highest priority will get chance first
• If we have two threads with same priority then we can't expect exact execution order it
depends on Thread scheduler ,which thread will get chance(That is taken care by Thread
scheduler what algorithm is used by scheduler so its unexpected
Thread class define get and set() methods to set the priorities of
Thread i.e.
public final int getPriority()
public final void setPriority(int p)
Default Priority
What is the default priority of a Thread?
For all remaining Threads default priority will be inherited from parent to
child
i.e. whatever priority parent has ,same priority will be there for the child
Thread
If Parent Priority is 10 automatically child priority will be 10
• sleep(milliseconds)---Suspends the thread for given milliseconds of time and define in try and catch block and
sleep Thread.sleep(1000)
is a static method
• join(): This is also a static method
Waits the thread to complete its process and used in multithreading and used with try and catch
clause
• getId() : used to get the ID of a thread
• getName() : Returens name of a thread and name of a thread starts with 0
• setName(String) –Thread name will be replaced with given string
• getPriority(): Priority ranges from 1 to 10 and by default priority of every thread is 5
• yield()
• Join()
• Sleep()
• If any thread executes unlimited then immediately it will pass execution to give chance
to remaining waiting threads of same priority and if no waiting thread, then the same
thread will continue its execution
• If all waiting threads having low priority, then same thread can continue its execution
• Suppose IF multiple waiting threads are there t1,t2,t3,t4 with same priority then which
thread will get the chance,that will be taken care by JVM thread scheduler not by
programmer
• Suppose currently tx thread executing with priority 7 then t1,t2,t3,t4 are all in the waiting stage with the priority 7
• Now this thread called Thread.yield () then this thread tx ,leave the processor to give the chance to remaining
thread of same priority
• When processor allocated to the t1 immediately tx will also come to waiting stage
• It depends on the thread scheduler when tx will get the chance again Programmer cannot expect result
the thread which is yielded, when it will get the chance once again it depends on thread scheduler and we cant
expect exactly depends on scheduler.
• Public static native (methods which are implemented in non java) void yield()
• public static native void yield() //complete prototype of yield()
• Purpose of yield() method causes to pause current executing thread to give the chance of remaining
• Waiting threads same priority
class thread1 extends Thread
{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println("Child Thread");
Thread. Yield();
}//this line executed by child method child thread always live in the processor, so our other thread is main()
}}
class thread2
{
public static void main(String args[])
{
thread1 th=new thread1();
th.start();//After this thread we have two threads main and child but //child thread invoking always yield ()
//and give the chance to remaining thread //and here remaining thread is main() and main() thread gets
more no //of times
for(int i=0;i<10;i++)
{
System.out.println("Main Thread");}}} //save it as thread2.java
Join()
• If a thread wants to wait until completion of certain thread then we use join()
• If Thread1 wants to wait until thread2 completes execution
• If t1 is there and t2 is there and both are executing and t1 wants to wait until completing t2 then t1 has
to call join() method.
• If a thread wants to wait until completing some other thread then we should go for join() method
• For Example if a thread t1 wants to wait until completing t2 then t1 has to call t2.join()
• If t1 executes t2.join() then immediately t1 will be entered into waiting state until t2 completes and once
a t2 completes then t1 can continue its execution
• Wedding card printing Thread(t2) has to wait until venue fixing thread (t1) completion hence t2 has to
call t1.join()
• Wedding card distribution Thread(t3)has to call until Wedding card printing thread(t2)completion hence
t3 has to call t2.join()method
Prototype of join() method
public final void join()
public final void join(long millisecond)//I will wait till this much millisecond
public final void join(long millisecond int nanoseconds)// I will wait till this much
milliseconds and this much nanoseconds
class thread2
{
public static void main(String args[])
{
thread1 th=new thread1();//create two objects of the same class
thread1 th1=new thread1();
th.start();
try
{
th.join();//In the absence of join two threads were running simultaneously but using join we are joining
the //threads so th1 will be waiting until the one thread th completes execution
}
catch(Exception e)
{
}
th1.start();//two threads will be running here
}
}
Cont…
}
}
catch(InterruptedException e)
{
System.out.println(“Main Thread Interrupted”);
}
System.out.println(“Main Thread Exiting”);}}
child Thread:Thread[Demo Thread,5,main]
Main thread : 5
child thread :4
child thread :4
Main thread : 4
child thread :3
child thread :2
Main thread : 3
child thread :3
child thread :1
Main thread : 2
child thread :2
child thread :1
for(int i=1;i<=5;i++)
}
System.out.println(“From thread A :i=“i);
}
System.out.println(“Exit from A”);
}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
{
System.out.println(“\t from thread B:j=“+j);
}
}
class C extends Thread
{
public void run()
{
for(int k=1;k<5;k++)
{
System.out.println(“From thread C:k=“+k);
}
System.out.println(“Exit from C”);
}
public static void main(String args[])
{
new A().start();
new B().start();
new C().start();
}
}
Thread Methods
Methods with Description
Sets the priority of this Thread object. The possible values are between 1 and 10.