Menu
Question Index
...

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);
        timer.schedule(new Sleep(), 0);
        timer.scheduleAtFixedRate(new Walk(), 1000, 10000);
        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-"); }
}


EAT-SLEEP-EAT-EAT-WALK-EAT-
EAT-SLEEP-WALK-EAT-EAT-WALK-EAT-
WALK-EAT-EAT-WALK-EAT-
Output can not be determined
Compilation Error or Runtime Error

Doubts

Problems

Topic: Java Timer Class And Java TimerTask Class

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App