freetimestudys@gmail.com          +91-7097633380
  Login Here                        

Thread Schedular

   Thread scheduler is a part of the JVM. It decides which thread is executed first and which thread is executed next.
  Only one thread is executed at a time.
  We can’t expect exact behavior of the thread scheduler it is JVM vendor dependent. So we can’t expect output of the multithreaded examples we can say the possible outputs.
   Thread Scheduler mainly uses preemptive (or) time slicing to schedule the threads.
Preemptive scheduling:-
In this highest priority task is executed first after this task enters into waiting state or dead state then only another higher priority task come to existence.
Time Slicing Scheduling:-
A task is executed predefined slice of time and then return pool of ready tasks. The scheduler determines which task is executed based on the priority and other factors.

Particular task is performed by the number of threads:-

1) Particular task is performed by the number of threads here number of threads(t1,t2,t3) are executing same method (functionality).
2) In the above scenario for each and every thread one stack is created. Each and every method called by particular Thread the every entry stored in the particular thread stack.

multiple threads are performing multiple operation

import java.util.*;
class MyThread extends Thread
{
public void run()
{
System.out.println("freetimestudy task");
}
}
class Test
{
public static void main(String[] args)
{
MyThread t1=new MyThread();
MyThread t2=new MyThread();
MyThread t3=new MyThread();
t1.start();
t2.start();
t3.start();
}
}

intput and output

output:freetimestudy task freetimestudy task freetimestudy task

multiple threads are performing multiple operation.

class MyThread1 extends Thread
{
public void run()
{
System.out.println("mythread1 task");
}
}
class MyThread2 extends Thread
{
public void run()
{
System.out.println("mythread2 task");
}
}
class MyThread3 extends Thread
{
public void run()
{
System.out.println("Mythread3 task");
}
}
class Test
{
public static void main(String[] args)
{
MyThread1 t1=new MyThread1();
MyThread2 t2=new MyThread2();
MyThread3 t3=new MyThread3();
t1.start();
t2.start();
t3.start();
}
}

intput and output

output:mythread1 task mythread2 task Mythread3 task