selfish thread
DESCRIPTION
This presentation is a glimpse of Selfish Thread.Rate it as per your perceptionTRANSCRIPT
A PRESENTATION ON SELFISH THREAD
CREATED BY:ANKITA TRIPATHI
SELFISH THREAD
The selfish thread is used to describe threads that do not ever call yield() or sleep() to allow other threads to run.
let each thread sleeps for a short amount of time. This gives the other thread a chance to run.
Every thread should occasionally yield control to other thread, otherwise the thread is selfish.
On some platforms ,a selfish thread can prevent other threads from making progress.
EXAMPLE
public class Runner extends Thread {Runner(int num){
threadNum = num;tick = 1;}
public void run() { while (tick < 4000000) {
if((tick % 100000) == 0) System.out.println("Tick number " +
tick + "of runner " + threadNum); tick++; } } int threadNum; int tick;}
Main method
class Race{
public static void main(String[] args){
Thread runner1 = new Runner(1);Thread runner2 = new Runner(2);runner1.start();runner2.start();
}}
WORKING
The while loop in the run() method is in a tight loop.
Once the scheduler chooses a thread with this thread body for execution, the thread never voluntarily relinquishes control of the CPU
The thread continues to run until the while loop terminates naturally or until the thread is preempted by a higher priority thread.
This thread is called Selfish thread.
Selfish output Tick number 100000 of runner 1 Tick number 200000
of runner 1 Tick number 300000 of runner 1 Tick number 400000
of runner 1 Tick number 500000 of runner 1 Tick number 100000
of runner 2 Tick number 200000 of runner 2 Tick number 300000
of runner 2 Tick number 400000 of runner 2 Tick number 500000
of runner 2 Tick number 600000 of runner 2 Tick number 700000
of runner 2 Tick number 800000 of runner 2 Tick number 900000
of runner 2 Tick number 600000 of runner 1 Tick number 700000
of runner 1 Tick number 800000 of runner 1 Tick number 900000
of runner 1