Thursday, January 24, 2013

MultiThreading Java Interview Question




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() mehod 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?

No.

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 backgrouund.

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 synchronised?

a. Method can be declared as synchronised.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?

NORM_PRIORITY

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. 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?

Synchronisation

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?
MAX_PRIORITY,MIN_PRIORITY and NORMAL_PRIORITY

10,1,5

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?

A 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.

No comments:

Post a Comment