Sunday, May 20, 2018

Java Thread Interview Questions

Leave a Comment

Java Thread Interview Questions

What are the two types of multitasking ?

a. Process-based.

b. Thread-based.

What is a Thread ?

A thread is a single sequential flow of control within a program.

What are the two ways to create a new thread?

a.Extend the Thread class and override the run() method.

b.Implement the Runnable interface and implement the run() method.

If you have ABC class that must subclass XYZ class, which option will you use to create a thread?

I will make ABC implement the Runnable interface to create a new thread, because ABC class will not be able to extend both XYZ class and Thread class.

Which package contains Thread class and Runnable Interface?

java.lang package

What is the signature of the run() method in the Thread class?

public void run()

Which methods calls the run() method?

start() method.

Which interface does the Thread class implement?

Runnable interface

What are the states of a Thread ?

Ready,Running,Waiting and Dead.

Where does the support for threading lie?

The thread support lies in java.lang.Thread, java.lang.Object and JVM.

In which class would you find the methods sleep() and yield()?

Thread class

In which class would you find the methods notify(),notifyAll() and wait()?

Object class

What will notify() method do?

notify() method moves a thread out of the waiting pool to ready state, but there is no guaranty which thread will be moved out of the pool.

Can you notify a particular thread?


What is the difference between sleep() and yield()?

When a Thread calls the sleep() method, it will return to its waiting state. When a Thread calls the yield() method, it returns to the ready state.

What is a Daemon Thread?

Daemon is a low priority thread which runs in the background.

How to make a normal thread as daemon thread?

We should call setDaemon(true) method on the thread object to make a thread as daemon thread.

What is the difference between normal thread and daemon thread?

Normal threads do mainstream activity, whereas daemon threads are used low priority work. Hence daemon threads are also stopped when there are no normal threads.

Give one good example of a daemon thread?

Garbage Collector is a low priority daemon thread.

What does the start() method of Thread do?

The thread’s start() method puts the thread in ready state and makes the thread eligible to run. start() method automatically calls the run () method.

What are the two ways that a code can be synchronized?

a. Method can be declared as synchronized.

b. A block of code be sychronised.

Can you declare a static method as synchronized?

Yes, we can declare static method as synchronized. But the calling thread should acquire lock on the class that owns the method.

Can a thread execute another objects run() method?

A thread can execute it’s own run() method or another objects run() method.

What is the default priority of a Thread?

If You Enjoyed This, Take 5 Seconds To Share It


Post a Comment