Interview Questions 1 - 10  «Prev Next»

Java Thread Methods Questions

  1. What does the Sleep method do to threads in Java?

    Answer:
    The Thread.sleep() method effectively pauses the current thread for a given period of time.

  2. What happens when the wait() method is called?

    Answer:
    1. The wait() method causes a thread to release the lock it is holding on an object; allowing another thread to run
    2. the wait() method is defined in the Object class and can only be invoked from within synchronized code
    3. The wait method should always be wrapped in a try block as it throws IOExceptions

  3. Why are wait(), notify() and notifyAll() methods defined in the Object class?

    Answer:
    The purpose of the wait(), notify() and notifyAll() methods is to temporarily pause and resume the execution of code in an object. Typically the host object is not in a state where it can proceed with a method call it has been given and the thread of execution must literally wait for the object to return to a ready state. A common example would be a limited pool or store of objects where you must wait for a storage slot to be released or an object to be returned to the pool before you can use it. The act of waiting is associated with the Object class because any subclass may need to wait for a ready state to occur (Java is fundamentally a multi-threaded language). The waiting process acts on a single thread of execution, but the wait mechanism expects that multiple threads may be waiting for the same object.
    The wait() and notify() methods are hosted by the Object class so that the Java Virtual Machine can manage the wait set of threads through the objects they are waiting for.

  4. Why are there separate wait() and sleep() methods?

    Answer:
    The static Thread.sleep(long) method maintains control of the thread execution but delays the next action until the sleep time expires.
    The wait() method gives up control over thread execution indefinitely so that other threads can run.

  5. What can happen when wait is called?

    Answer:
    When wait() is called, the thread becomes disabled for scheduling and lies dormant until one of four things occur:
    1. another thread invokes the notify() method for this object and the scheduler arbitrarily chooses to run the thread
    2. another thread invokes the notifyAll() method for this object
    3. another thread interrupts this thread
    4. the specified wait() time elapses

  6. What does the IOException method do when handling exceptions?

    Answer:
    The exception thrown is IOException and the catch block handles the IOException.

  7. What is the difference between Thread and Runnable types?

    Answer:
    A Java Thread controls the main path of execution in an application.
    When you invoke the Java Virtual Machine with the java command, it creates an implicit thread in which to execute the main() method.
    The Thread class provides a mechanism for the first thread to start-up other threads to run in parallel with it.
    The Runnable interface defines a type of class that can be run by a thread.
    The only method it requires is run(), which makes the interface very easy to fulfill by extending existing classes.
    A runnable class may have custom constructors and any number of other methods for configuration and manipulation.

  8. How do Java threads make the environment asynchronous?

    Answer:
    The thread mechanism in Java begins with the main entry point thread, which the runtime environment creates to start a Java program. The initial thread creates secondary threads, each one runs independently of the other. The Java virtual machine manages the execution of the threads so they behave as if they all run at the same time. In fact, each thread briefly takes turns at execution. In its simplest form there may be no communication or synchronization between multiple threads in a Java program and they each run to completion independently of each other. In this respect Java threads are fundamentally asynchronous. There is no master clock that governs when threads will run and when they synchronize variables to catch-up with each other. It is often necessary and more useful if threads
    1. do check ready states before progressing
    2. synchronize read and write access to shared variables and
    3. call-back to each other when their work is done.
      This is where the synchronized keyword and the various sleep(), wait() and notify() methods are used to more closely schedule the interaction between asynchronous threads.

  9. What is a Runnable object and a Runnable argument?

    Answer:
    A Runnable object is one that implements the Runnable interface, which is the type used to execute new threads.
    The Runnable interface only has one method, run(), which must be implemented by a Runnable class.
    In Java, an argument is a primitive value or object reference that is passed to a constructor or method, defined in the method signature.
    A Runnable argument would be a constructor or method argument that is declared to be a Runnable type.
    The constructor of the Thread class is the most obvious example.
    public Thread(Runnable target);    
    

    This constructor requires a Runnable type argument to be passed when a Thread is instantiated.

  10. How does the run() method in Runnable work?

    Answer:
    It may help to think of the run method like the main method in standard single threaded applications. The run method is a standard entry point to run or execute a class. The run method is normally only executed in the context of an independent Thread, but is a normal method in all other respects.