Thread priorities and Thread Schedule.

In this article,Thread priorities and Thread Schedule is explained in detail with examples.
When Multiple threads are getting executed in the same program,we can change priorities (Thread priorities and Thread Schedule)of any particular thread using the following methods.
setPriority(int level);
intlevel=getPriority();
According to JVM we have 1--10 priority level using the following Data members in thread class.
public static final int MIN_PRIORITY;(level-1)
public static final int NORM_PRIORITY;(level-5)
public static final int MAX_PRIORITY;(level-10)
and default priority level for all threads is 5 (NORM_Priority)---(From topic Thread priorities and Thread Schedule)
Ex
//program related to Thread priorities and Thread Schedule-Thread priority.
class ExampleThread7 implements Runnable
{
public void run()
{
while(true)
{
System.out.println(Thread.currentThread().getName());
}
}
}
public class Thread7
{
public static void main(String args[])
{
ExampleThread7 t=new ExampleThread7();
Thread t1=new Thread(t,"AAAA");
Thread t2=new Thread(t,"BBBB");
t2.setPriority(t1.getPriority()+5);
t1.setPriority(Thread.MIN_PRIORITY);
t1.start();
t2.start();
}
}
Output
Thread7-output-javaform
                                 To get the code file use me.

Thread-Schedule.
Scheduling means giving CPU time, running state to the threads which are in Queue (Read-to-Run state) this is done by JVM using pre-defined scheduling algorithms (based on priorities).However,a developer of java program can also schedule execution of threads using following two methods namely
  • Sleep(long).
  • Yield().
Sleep(long)
Thus method takes the thread to sleep state,for given number of milliseconds.
Ex
//program related to Thread priorities and Thread Schedule-Sleep
class ExampleThread6 implements Runnable
{
public void run()
{
while(true)
{
System.out.println(Thread.currentThread().getName());
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
return;
}
}
}
}
public class Thread6
{
public static void main(String args[])
{
ExampleThread6 t=new ExampleThread6();
Thread t1=new Thread(t,"AAAA");
Thread t2=new Thread(t,"BBBB");
t1.start();
t2.start();
}
}
Output
Sleep(long)-javaform
                               To get the code file use me.

Yield()
This method takes the thread from ready to run state, voluntarily giving its CPU time for other threads to execute.
Note
These two above methods are used in Run method only.
Ex
//program related to Thread priorities and Thread Schedule- Yield.
class ExampleThread5 implements Runnable
{
public void run()
{
while(true)
{
System.out.println(Thread.currentThread().getName());
Thread.yield();
}
}
}
public class Thread5
{
public static void main(String args[])
{
ExampleThread5 t=new ExampleThread5();
Thread t1=new Thread(t,"AAAA");
Thread t2=new Thread(t,"BBBB");
t1.start();
t2.start();
}
}
Output
Yield-javaform
                           To get the code file use me.

Note
Sleep(long) throws interrupted exception,hence when we use this method,we provide Exception handling through try and catch blocks.

Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing.

Do check my new startup Surprise Planners in HyderabadLavish Surprises our services are surprise party planners in Hyderabadsurprise giftssurprise birthday party planners Hyderabad, Wedding anniversary surprisessurprise planners Hyderabad.

Hi Friends, Please comment down your views in the comment section below. Thank you...

No comments:

Connect with Us by Subscribing here for more Updates..!

* indicates required