Java Interview Questions


Q:

How to create multithreaded program? Explain different ways of using thread?

 
A: There are two ways to create multithreaded program. First one is by extending the Thread class. The other way is by making the class implement "Runnable" interface.

The latter is more advantageous, because while going for multiple inheritance, only interface can help. . If the programme is already inheriting a different class, then you have to go for Runnable Interface. Otherwise you can extend Thread class. Also, if you are implementing interface, it means you have to implement all methods in the interface. If the class is not extending any other class, then you can extend Thread class as it will save few lines of coding.
.

Q:

What is synchronization?

 
A: With respect to multithreading, Synchronization is a process of controlling the access of shared resources by multiple threads in such a manner that only one thread can access a particular resource at a time. In non synchronized multithreaded application, it is possible for one thread to modify a shared object while another thread is in the process of using or updating the object's value. Synchronization prevents such type of data corruption which may otherwise lead to dirty reads and significant errors.

You can either synchronizing a function or synchronizing a piece of code.

E.g. synchronizing a function:

public synchronized void Method1 () {
// method code.
}
E.g. synchronizing a block of code inside a function:
public Method2 (){
synchronized (this) {
// synchronized code here.
}
}
Synchronized blocks place locks for shorter periods than synchronized methods.

.

Q:

What is mutual exclusion? How can you take care of mutual exclusion using Java threads?

 
A: Mutual exclusion is a phenomenon where no two processes can access critical regions of memory at the same time. Using Java multithreading we can arrive at mutual exclusion. For mutual exclusion, you can simply use the synchronized keyword and explicitly or implicitly provide an Object to synchronize on. The synchronized keyword can be applied to a class, to a method, or to a block of code. There are several methods in Java used for communicating mutually exclusive threads such as wait( ), notify( ), or notifyAll( ). For example, the notifyAll( ) method wakes up all threads that are in the wait list of an object
.
Q:

What invokes a thread's run() method?

 
A: After a thread is started, via its start() method of the Thread class, the JVM invokes the thread's run() method when the thread is initially executed.
.
Q:

What is deadlock?

 
A: When two threads are waiting for each other and can’t proceed until the first thread obtains a lock on the other thread or vice versa, the program is said to be in a deadlock.
.
Q:

What’s the difference between the methods sleep() and wait()?

 
A: The sleep method is used when the thread has to be put aside for a fixed amount of time. Ex: sleep(1000), puts the thread aside for exactly one second. The wait method is used to put the thread aside for up to the specified time. It could wait for much lesser time if it receives a notify() or notifyAll() call. Ex: wait(1000), causes a wait of up to one second. The method wait() is defined in the Object and the method sleep() is defined in the class Thread.
.

Java Interview Questions