Menu
Topics Index
...
`

Life And Death2372


If you need explanation Read this topic

If you need Answer Take test on this topic

Life And Death2372
What will be the output of the following program?
import java.util.*;
public class LifeAndDeath {
    public static void main(String[] args) throws InterruptedException {
        Timer timer = new Timer();
        timer.schedule(new Eat(), new Date(), 5000);
        Walk walk = new Walk();
        timer.schedule(walk, new Date(new Date().getTime() + 10000));
        timer.schedule(new Sleep(), 0);
        timer.scheduleAtFixedRate(new Walk(), 1000, 10000);
        Thread.sleep(4000);
        walk.cancel();
        Thread.sleep(17500);
        timer.cancel();
    }
}
class Eat extends TimerTask {
    public void run() { System.out.print("EAT-"); }
}
class Sleep extends TimerTask {
    public void run() { System.out.print("SLEEP-"); }
}
class Walk extends TimerTask {
    public void run() { System.out.print("WALK-"); }
}

A. EAT-SLEEP-WALK-EAT-EAT-WALK-EAT-
B. EAT-SLEEP-EAT-EAT-WALK-EAT-
C. EAT-SLEEP-WALK-EAT-EAT-WALK-EAT-EAT-WALK-
D. Output can not be determined
E. Compilation Error or Runtime Error
Topic: Java Timer Class And Java TimerTask Class

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Answer is option C :: firstly the class Eat thread is excuted and then immediately Sleep..then walk...then the current thread sleeps for 4000ms..then EAT thread is  excuted!! then again the current thread that is EAT is into sleep  and the WALK is excuted since it is canceld..then again EAT is exuted...and finally WALK is exuted and then the timer is cancelled.

Posted by Shaik Nyamath    2014-01-09 12:59:51


Congratulations Shaik Nyamath . You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-01-10 04:18:49


© meritcampus 2019

All Rights Reserved.

Open In App