Java - Threads - Interview Questions

What are the different ways to define threads in Java programming language?

 FAQKey Concept

There are two ways to define and run threads in Java programming language.

First way to define threads is to extend from java.lang.Thread class, and override its run() method. You instantiate and start this thread by creating a new instance of your class and calling the start() method

Second way to define threads is to implement java.lang.Runnable interface, and implement its run method. You can instantiate and start this thread by creating a creating a new instance of your class that implements Runnable interface, and passing this instance of Runnable to a new Instance of Thread.

Defining a thread by implementing the Runnable is the preferred way, since if you make your class extend from the Thread class, you will not be able to extend from any other class.

/** Extending from java.lang.Thread */
//Define
Class MyThread extends Thread {
public void run() {...}
}
//Instantiate
MyThread t = new MyThread();
//Start
t.start();
/** Implementing java.lang.Runnable interface */
//Define
Class MyRunnable implements Runnable {
public void run() {...}
}
//Instantiate
MyRunnable r = new MyRunnable();
Thread t = new Thread(r);
//Start
t.start()

Describe the life-cycle of threads. What are the different states of threads? How does the thread transition from one state to the other?

 FAQKey Concept

A thread can be in one of five states

New - A thread is in 'New' state when it has been instantiated, but the start() method is not yet called on the thread instance. In this state the thread is a Thread object, but it is not yet a thread of execution.

Runnable - A thread is in Runnable state when the start() method has been called on the thread instance but it is not yet picked up by the thread scheduler for execution.

Running - A thread is in Running state when it is currently executing . The thread scheduler picks up the thread from the thread pool and executes it.

Waiting, Blocked, Sleeping - In this state the thread is not eligible for execution and is not in the running pool. A thread may be ineligible for execution for a variety of reasons. The thread may be blocked waiting for an IO resource or on a lock. The thread may be sleeping since the code tells it to sleep for a specific period of time. Or the thread may be waiting on another thread to send it a notification.

Dead - A thread is in dead state when the execution of its run() method is complete. Once a thread is dead it cannot be run again, hence a thread cannot transition from dead state to any other state.

How do you pause a thread from executing for a certain period of time?

 FAQKey Concept

You can pause the execution of a thread for a certain period of time by calling its sleep() method and passing the time that the thread has to stop executing.

Though you specify the time period that the thread should sleep for, there is no guarantee that the thread will pause executing exactly for that period of time.

The thread may start executing sooner than the specified time - if it gets an interrupt from another thread. Or the thread may start executing later than the specified time - After the specified sleep period of time has elapsed, the thread is put into the runnable pool of threads from where it will be picked up by the scheduler. The scheduler may pick up this thread for execution immediately or if there are higher priority threads then it will be executed later.

What is the function of join() method in Thread class in Java programming language?

 FAQKey Concept

The join() method allows one thread to wait for the completion of another thread. For example - If t is a Thread instance, then calling t.join() in the current thread, say the main thread, will cause the main thread to wait until the t thread has completed its execution.

What is the function of sleep() method in Thread class in Java programming language?

 FAQ

Sleep() method is used to delay the execution of the thread for a specific period of time...

*** See complete answer in the Java Interview Guide.

Java Interview Guide has over 250 REAL questions from REAL interviews. Get the guide for $15.00 only.
 
BUY EBOOK
 

What is the function of yeild() method in Thread class in Java programming language?

 FAQ

Yeild() method stops a running thread so that other threads which are in the waiting state and are of the same priority as the running thread will get a chance to execute...

*** See complete answer in the Java Interview Guide.

In the case of multiple threads, how do you prevent data corruption of shared data?

 FAQKey Concept

Java programming language provides the 'synchronized' keyword which allows only one thread at a time to access the shared resource...

*** See complete answer in the Java Interview Guide.

What are locks or monitors in Java programming language?

 FAQ

Synchronization is based on internal entities called intrinsic locks or monitor locks. Every object has an intrinsic lock associated with it...

*** See complete answer in the Java Interview Guide.

What do you understand by Reentrant Synchronization in Java programming language?

 FAQ

A thread cannot acquire a lock that is held by another thread. But a thread can acquire a lock that it already owns...

*** See complete answer in the Java Interview Guide.

What is the function of wait(), notify() and notifyAll() methods in Java programming language?

 FAQ

Java programming language provides the wait(), notify() and notifyAll() methods that facilitate thread to thread communication.

wait() method lets a running thread, say Thread A, stop execution and wait on a specific action to complete in another thread, say Thread B. Thread A releases its locks and goes into waiting state while waiting for the specific action to complete on Thread B...

*** See complete answer in the Java Interview Guide.

 
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
 

Java - Interview Questions

Java - Object Oriented ProgrammingJava - Objects & ClassesJava - Data TypesJava - VariablesJava - StringsJava - ArraysJava - CollectionsJava - ReflectionJava - Lambda ExpressionsJava - StreamsJava - GenericsJava - ExceptionsJava - IOJava - ThreadsJava - ConcurrencyJava - JDBCJava - NetworkingJava - SecurityJava - JVM InternalsJava - PerformanceJava - New in Java 8Java - New in Java 9Java - New in Java 10Java - New in Java 11
 
MASTER Java  

Top ranked courses to help you master Java skills.
Java Programming Masterclass

iconicon

Offered By - Tim Buchalka
Platform - Udemy
Rating - * * * * *
Students Enrolled - 575,000 +

RECOMMENDED RESOURCES
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example