Topics Index

Life And Death

If you need explanation Read this topic

If you need Answer Take test on this topic

Life And Death
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);
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-"); }

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 B...used to schedule a thread to be executed at certain time in future

TimerTask is an abstract class that implements Runnable interface and we need to extend this class to create our own TimerTask that can be scheduled using java Timer class.

we have 3 classes Eat n Sleep and Walk which are extending the class TimerTask.. all these three classes are overriding the method run() Bcz TimerTask is an abstract class
if one thread is executing than timeer wil wait until it finishes it;s execution..after completion of its executin than it wil give chacne another thread...

here we are calling the method schedule() by passing the instances of Eat n Date n duration

1st arg---Object of TimeTask
2nd arg---on which date it shuld start execution
3rd arg:milliseconds between successive task executions

here we are passing Eat clas Object so that it wil print as Eat

next we are passing () it wil print as Sleep()...

scheduleAtFixedRate() this metod is used to execute repeatly in fixed amount of delay time... it wil print as as Walk-

Posted by Uday Kumar    2014-10-24 08:50:15


Posted by Shubham Bansal    2014-10-24 09:07:10

This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Maheshwari Natarajan. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2014-10-25 03:04:38

© meritcampus 2019

All Rights Reserved.

Open In App