selfish thread

7
A PRESENTATION ON SELFISH THREAD CREATED BY: ANKITA TRIPATHI

Upload: ankita-tripathi

Post on 06-May-2015

33 views

Category:

Education


0 download

DESCRIPTION

This presentation is a glimpse of Selfish Thread.Rate it as per your perception

TRANSCRIPT

Page 1: Selfish thread

A PRESENTATION ON SELFISH THREAD

CREATED BY:ANKITA TRIPATHI

Page 2: Selfish thread

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.

Page 3: Selfish thread

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;}

Page 4: Selfish thread

Main method

class Race{

public static void main(String[] args){

Thread runner1 = new Runner(1);Thread runner2 = new Runner(2);runner1.start();runner2.start();

}}

Page 5: Selfish thread

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.

Page 6: 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

Page 7: Selfish thread