Sunday, May 20, 2018

Java Thread Interview Questions

Leave a Comment

Java Thread Interview Questions

What is threaded programming and when is it used?

Threaded programming is normally used when a program is required to do more than one task at the same time. Threading is often used in applications with graphical user interfaces; a new thread may be created to do some processor-intensive work while the main thread keeps the interface responsive to human interaction.
The Java programming language has threaded programming facilities built in, so it is relatively easy to create threaded programs. However, multi-threaded programs introduce a degree of complexity that is not justified for most simple command line applications.

Why not override Thread to make a Runnable?

There is little difference in the work required to override the Thread class compared with implementing the Runnable interface, both require the body of the run() method. However, it is much simpler to make an existing class hierarchy runnable because any class can be adapted to implement the run() method. A subclass of Thread cannot extend any other type, so application-specific code would have to be added to it rather than inherited.
Separating the Thread class from the Runnable implementation also avoids potential synchronization problems between the thread and the run() method. A separate Runnable generally gives greater flexibility in the way that runnable code is referenced and executed.

What’s the difference between a thread’s start() and run() methods?

The separate start() and run() methods in the Thread class provide two ways to create threaded programs. The start() method starts the execution of the new thread and calls the run() method. The start() method returns immediately and the new thread normally continues until the run() method returns.
The Thread class’ run() method does nothing, so sub-classes should override the method with code to execute in the second thread. If a Thread is instantiated with a Runnable argument, the thread’s run() method executes the run() method of the Runnable object in the new thread instead.

Depending on the nature of your threaded program, calling the Thread run() method directly can give the same output as calling via the start() method, but in the latter case the code is actually executed in a new thread.

Can I implement my own start() method?

The Thread start() method is not marked final, but should not be overridden. This method contains the code that creates a new executable thread and is very specialised. Your threaded application should either pass a Runnable type to a new Thread, or extend Thread and override the run() method.

What’s the difference between Thread and Runnable types?

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 to fulfil by extending existing classes. A runnable class may have custom constructors and any number of other methods for configuration and manipulation.

How does the run() method in Runnable work?

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.

What is a deadlock?

A condition that occurs when two processes are waiting for each other to complete before proceeding. The result is that both processes wait endlessly.

What are all the methods used for Inter Thread communication and what is the class in which these methods are defined?
A) a. wait(),notify() & notifyall()

b. Object class

What is the mechanisam defind in java for a code segment be used by only one Thread at a time?


What is the procedure to own the moniter by many threads?

Its not possible. A monitor can be held by only one thread at a time.

What is the unit for 500 in the statement, obj.sleep(500);?

500 is the no of milliseconds and the data type is long.

What are the values of the following thread priority constants?


What is the default thread at the time of starting a java application?

main thread

The word synchronized can be used with only a method. True/ False?

False. A block of code can also be synchronised.

What is a Monitor?

A monitor is an object which contains some synchronized code in it.

What are all the methods defined in the Runnable Interface?

only run() method is defined the Runnable interface.

How can i start a dead thread?

A dead Thread cannot be started again.

When does a Thread die?

Thread dies after completion of run() method.

What does the yield() method do?

The yield() method puts currently running thread in to ready state.

What exception does the wait() method throw?

The java.lang.Object class wait() method throws “InterruptedException”.

What does notifyAll() method do?

notifyAll() method moves all waiting threads from the waiting pool to ready state.

What does wait() method do?

wait() method releases CPU, releases objects lock, the thread enters into pool of waiting threads.
If You Enjoyed This, Take 5 Seconds To Share It


Post a Comment