Menu
Topics Index
...
`

Waiting Thread


If you need explanation Read this topic

If you need Answer Take test on this topic

Waiting Thread
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();
    @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 Runitme 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.

Option C
as wait() method expects a parameter for time in milliseconds to wait for
Exception rises in try block is catched

Posted by Sasikanth Sharma    2014-11-10 03:48:13


ans is c

Posted by Raju VarshnEy    2014-11-10 03:48:56


ans is c.

Posted by Shubham Bansal    2014-11-10 03:51:55


Answer is C....All the claases that extends Thread are als Threads which means CounterClass is a thread since it extends a Thread Class and counerclass possess all the the behaviuor that thread class can exhibit...Also the CounterClass overrides run method......When we use @Override annotation to a method, it let compiler know that we are overriding it from super class....I n the main method we are creating an instance of counterClass and calling run method on that object..Run method does its execution in the current thread but does not  start the execution in a new thread unlike start method....In the run method we are printing "Thread Entered" and we are calling t.wait() where t is a new instance of a thread....Point here is before calling the wait method on any instance we have to  obtain a lock on that object....since we are not doing here it throws a IllegalMonitorStateException and we can catch sub class of all the exceptions through any of its parent class exceptions here Catch block is executed since it encountered an exception and prints "Thread Interrupted".Thread Exiting will  also be printed since we are properly catching catching the exception and the program execution continues

Posted by Shashanka Mogaliraju    2014-11-10 05:56:56


ans c .in this program we have 2 classes .counterclass and main class-waitingthread,inside main class reference for the the counterClass class is created .by using run() method is called.CounterClass extends Thread class so it can use all the features of thread class.The Thread class encapsulates all of the information about a single thread of control running in a Java
environment. Thread objects are used to control threads in a multithreaded program.                 The execution of Java code is always under the control of a Thread object. The Thread class provides
a static method called currentThread() that can be used to get a reference to the Thread
object that controls the current thread of execution.                                                               In order for a Thread object to be useful, it must be associated with a method that it is supposed to run.
Java provides two ways of associating a Thread object with a method:                                       After a thread is started, it dies when one of the following things happens:
l The run() method called by the Thread returns.
l An exception is thrown that causes the run() method to be exited.
l The stop() method of the Thread is called.
l Declare a subclass of Thread that defines a run() method. When such a class is instantiated
and the object's start() method is called, the thread invokes this run() method.
l Pass a reference to an object that implements the Runnable interface to a Thread constructor.
When the start() method of such a Thread object is called, the thread invokes the run()
method of the Runnable object.                             in this program thread object cread.in run() method 1st sop prints thn try catch mechanism is handled to avoid run time exceptions.this wait() method throws 2 types of exceptions-IllegalMonitorStateException
If the method is called from a thread that does not hold this object's lock.
InterruptedException
If another thread interrupted this thread.catch block get executed bcz try throws exception.ouside try catch block ast sop aso get printd

Posted by Maheshwari Natarajan    2014-11-10 17:48:50


This dose is now closed and the winners are Sasikanth Sharma,  for 'First Correct Comment', Shashanka Mogaliraju, Maheshwari Natarajan,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Vikas Tiyagi. 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-11 00:58:33


© meritcampus 2019

All Rights Reserved.

Open In App