Monday, December 29, 2008

multithreading interview questions

1)What is Thread ?

A thread is an independent path of execution in a system.

2)What is the difference between the Thread and Runnable ?

Thread Class :: if you extend a Thread class , it does not allow to extend another class. A class extends the Thread class and overrides it's run() method.
Runnable interface :: if you implement Runnable interface ,you can extend another class. A class implements the Runnable interface, which has one method: run(). The class passes a reference to itself when it creates a thread. The thread then calls back to the run() method in the class.

3)What is multithreading and what are the methods for inter-thread communication and what is the class in which these methods are defined? -
Multithreading is the mechanism in which more than one thread run independent of each other within the process. wait (), notify () and notifyAll() methods can be used for inter-thread communication and these methods are in Object class.wait() : When a thread executes a call to wait() method, it surrenders the object lock and enters into a waiting state.notify() or notifyAll() : To remove a thread from the waiting state, some other thread must make a call to notify() or notifyAll() method on the same object.

4)What is the difference between sleep() and wait() in multithreading ?
the difference i think is that a wait() realeases the lock for the synchronized object butsleep() don' it may be interesting to implementthe behaviour of wait() and notify() and compare withsleep()....
Never use sleep when you are using synchronization, it could cause deadlocks.Instead, use wait as wait will release all your locks and thus reduce the chance of deadlock.Remember that your thread may be holding locks that you are unaware of so it's best to wait (not sleep).

5)What is deadlock?
- When two threads are waiting each other and can’t precede the program is said to be deadlock.

6)What is Demoniac Thread ?
A thread that exists/runs in the background.basically a low priority thread.garbage collector thread is an example for Deamon thread

7) What are the methods available in the Thread class?
1.isAlive() 2.join() 3.resume()4.suspend()5.stop()6.start()7.sleep()8.destroy()

No comments:

Post a Comment

Your Ad Here ]]>