Menu
Topics Index
...
`

Life And Death2373


If you need explanation Read this topic

If you need Answer Take test on this topic

Life And Death2373
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(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.

ans is E because Exception in thread "main" java.lang.IllegalStateException:   Task already scheduled or cancelled at java.util.Timer.sched at java.util.Timer.scheduleAtFixedRate at LifeAndDeath.main

Posted by Shubham Bansal    2014-11-03 12:44:36


Ans is A...Timerclass is used for threads to schedule the task for the future use..this wil thread safe..multiple threads can share a single Timer object without the need for external synchronization.

schedule()---->this wil used to schedule the specific task for the execution..

1st arg is which task should execute and 2nd Arg is at what time it should execute...

sleep() wil used to stop thread to execute for specific amount of time in milli sec

scheduleAtFixedRate()--->method is used to schedule the specified task for repeated fixed-rate execution, beginning at the specified time.

Posted by Shubham Bansal    2014-11-03 12:46:01


Ans is A...Timerclass is used for threads to schedule the task for the future use..this wil thread safe..multiple threads can share a single Timer object without the need for external synchronization.

schedule()---->this wil used to schedule the specific task for the execution..

1st arg is which task should execute and 2nd Arg is at what time it should execute...

sleep() wil used to stop thread to execute for specific amount of time in milli sec

scheduleAtFixedRate()--->method is used to schedule the specified task for repeated fixed-rate execution, beginning at the specified time.

Posted by Uday Kumar    2014-11-03 15:46:51


Shubham Bansal, please stop this practice of copying others comments. Be original and comment only if you understand. If you continue to do this, we have no other option but ban you from the contests.

Posted by Merit Campus    2014-11-04 04:14:01


This dose is now closed and the winners are Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Uday Kumar. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-11-04 04:16:06


© meritcampus 2019

All Rights Reserved.

Open In App