Menu
Topics Index
...
`

Waiting Thread2915


If you need explanation Read this topic

If you need Answer Take test on this topic

Waiting Thread2915
What will be the output of the following program?
public class WaitingThread{
    public static void main(String[] args) {
        CounterClass thread1 = new CounterClass(); // LINE A
        thread1.run();
    }
}
class CounterClass extends Thread {
    Thread t = new Thread(new CounterClass());
    @Override
    public void run() {
        System.out.println("Thread entered.");
        try {
            t.wait();
        } catch (Exception e) {
            System.out.println("Thread Interrupted.");
        }
        System.out.println("Thread exiting.");
    }
}

A. Thread entered.
Thread exiting.
B. Thread entered.
C. Thread entered.
Thread Interrupted.
Thread exiting.
D. Output can not determined
E. Compilation Error or Runtime Error
Topic: Java Inter Thread Communication With Example

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 E.....This program will throw stack OverFlow Error....Any instance of a CounterClass is a thread since CounterClass extends Thread.... line A of the program will try to create a instance of CounterClass but in this process it will actually try to initialse the instance variable t wherein we are again creating an instance of CounterClass and again variable t initialisation at runtime happens and again another object of CounterClass creation happens and this goes on happening until we run out of stack space allocated to the thread which will result in stack overflow error

Posted by Shashanka Mogaliraju    2014-11-11 08:54:50


e

Posted by Shubham Bansal    2014-11-11 08:58:09


Ans is E...StackOverFlow Error...Coming to the program here we CounterClass which is extending the Thread....

in the  main class we create Object for the CounterClass so that here we create an thread...but we didnt start the thread here..instead we are calling the run() sirectly with that object...

in Counter class we are creating the Object for the clss Thread by passing the CounterClass Objet...if we create an CounterClass Object than agian one more thread clss Object will create by passing the Counterclass...

again we are creaing the Counter class Object so same thing will happens.

here instance variable varaible t will store in the stack so every time when we create CounterClass Object than it wil store in the Stack.

The common cause for a stack overflow is a bad recursive call...

if we remove the Argument in Thread() than it wil work fine....here passing the Argument is not necessary...we will pass the Object to Thread class when v want to create threads using the Runnable...

Posted by Uday Kumar    2014-11-11 16:39:24


ans e..runtime error.                                              stackOverFlowError occur.This error is thrown to indicate that the application’s stack was exhausted, due to deep recursion.The StackOverflowError extends the VirtualMachineError class, which indicates that the JVM is broken, or it has run out of resources and cannot operate. Furthermore, the the VirtualMachineError extends the Error class, which is used to indicate those serious problems that an application should not catch. A method may not declare such errors in its throw clause, because these errors are abnormal conditions that shall never occur.                                in this program we have 2 classes in main class we created an object for counterclass.by using that referencr run() method directly called.this is entry point for the thread.counterclass extends thread class here thread is created with counterclass object.here instance varible t initialized with counterclass obj recursive in runtime after stack space exhausted it throws stackoverflowError

Posted by Maheshwari Natarajan    2014-11-11 17:13:40


This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Shashanka Mogaliraju. 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-12 03:59:04


© meritcampus 2019

All Rights Reserved.

Open In App