Java / J2EE - Interview Questions and Answers on Threads and Multithreading

Q1.  Explain multithreading in Java and its benefits ?

Ans. 

a. Multithreading provides better interaction with the user by distribution of task

b. Threads in Java appear to run concurrently, so it provides simulation for simultaneous activities.

The processor runs each thread for a short time and switches among the threads to simulate sim-ultaneous execution (context-switching) and it make appears that each thread has its own processor.By using this feature, users can make it appear as if multiple tasks are occurring simultaneously when, in fact, each is 
running for only a brief time before the context is switched to the next thread.

c. We can do other things while waiting for slow I/O operations.

In the java.iopackage, the class InputStreamhas a method, read(), that blocks until a byte is read from the stream or until an IOExceptionis thrown. The thread that executes this method cannot do anything elsewhile awaiting the arrival of another byte on the stream.

Q2.  Can constructors be synchronized in Java ?

Ans. No. Java doesn't allow multi thread access to object constructors so synchronization is not even needed.

Q3.  Why threads block or enters to waiting state on I/O?

Ans. Threads enters to waiting state or block on I/O because other threads can execute while the I/O operations are performed.

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

Ans. When a object invokes yield() it returns to ready state. But when an object invokes sleep() method enters to not ready state.

Q5.  What is the difference between time slicing and preemptive scheduling ?

Ans. In preemptive scheduling, highest priority task continues execution till it enters a not running state or a higher priority task comes into existence. In time slicing, the task continues its execution for a predefined period of time and reenters the pool of ready tasks.

Q6.  What is the initial state of a thread when it is created and started?

Ans. Ready state.

Q7.  Can a lock be acquired on a class ?

Ans. Yes, a lock can be acquired on a class. This lock is acquired on the class's Class object.

Q8.  What state does a thread enter when it terminates its processing?

Ans. When a thread terminates its processing, it enters the dead state.

Q9.  What is an object's lock and which object's have locks?

Ans. An object's lock is a mechanism that is used by multiple threads to obtain synchronized access to the object. A thread may execute a synchronized method of an object only after it has acquired the object's lock. All objects and classes have locks. A class's lock is acquired on the class's Class object.

Q10.  Different ways of implementing Threads in Java ?

Ans. Threads in Java can be implement either by Extending Thread class or implementing runnable interface.

Q11.  What is a Deadlock ?

Ans. When two threads are waiting each other and can’t precede the program is said to be deadlock.

Q12.  What is suspend() method used for ?

Ans. suspend() method is used to suspend the execution of a thread for a period of time. We can then restart the thread by using resume() method.

Q13.  Difference between suspend() and stop() ?

Ans. Suspend method is used to suspend thread which can be restarted by using resume() method. stop() is used to stop the thread, it cannot be restarted again.

Q14.  Explain Thread States ?

Ans. 

Runnable - waiting for its turn to be picked for execution by the thread schedular based on thread priorities.

Running - The processor is actively executing the thread code. It runs until it becomes blocked, or voluntarily gives up its turn. 

Waiting: A thread is in a blocked state while it waits for some external processing such as file I/O to finish.

Sleeping - Java threads are forcibly put to sleep (suspended) with Thread.sleep. they can resume using Thread.resume method.

Blocked on I/O - Will move to runnable after I/O condition like reading bytes of data etc changes.

Blocked on synchronization - Will move to Runnable when a lock is acquired.

Dead - The thread is finished working.

Q15.  Difference between yield() and sleeping()? 

Ans. When a task invokes yield(), it changes from running state to runnable state. When a task invokes sleep(), it changes from running state to waiting/sleeping state.

Q16.  What is a daemon thread? Give an Example ?

Ans. These are threads that normally run at a low priority and provide a basic service to a program or programs when activity on a machine is reduced. garbage collector thread is daemon thread.

Q17.  What is race condition ?

Ans. A source of possible errors in parallel programming, where one thread can cause an error in another thread by changing some aspect of the state of the program that the second thread is depending on (such as the value of variable).

Q18.  What is ThreadFactory ?

Ans. ThreadFactory is an interface that is meant for creating threads instead of explicitly creating threads by calling new Thread(). Its an object that creates new threads on demand. Using thread factories removes hardwiring of calls to new Thread, enabling applications to use special thread subclasses, priorities, etc.

Q19.  When you will synchronize a piece of your code?

Ans. When you expect your code will be accessed by different threads and these threads may change a particular data causing data corruption.

Q20.  Is runnable a Marker interface ?

Ans. No , it has run method declared.

Q21.  Explain Thread States ?

Ans. Runnable - waiting for its turn to be picked for execution by the thread schedular based on thread priorities.

Running - The processor is actively executing the thread code. It runs until it becomes blocked, or voluntarily gives up its turn. 

Waiting: A thread is in a blocked state while it waits for some external processing such as file I/O to finish.

Sleeping - Java threads are forcibly put to sleep (suspended) with Thread.sleep. they can resume using Thread.resume method.

Blocked on I/O - Will move to runnable after I/O condition like reading bytes of data etc changes.

Blocked on synchronization - Will move to Runnable when a lock is acquired.

Dead - The thread is finished working.

Q22.  Difference between yield() and sleeping()? 

Ans. When a task invokes yield(), it changes from running state to runnable state. When a task invokes sleep(), it changes from running state to waiting/sleeping state.

Q23.  What is the use of runnable interface if we can always create a new thread using Thread class ?

Ans. It's a choice to be made whether to use first approach ( Thread class ) or second approach ( runnable interface ) by the programmer. The second facility has been given for cases where your class is already extending some parent class and hence cannot extend another class ( for Thread ) as Java doesn't allow multiple inheritance.

Q24.  Why do we need Thread class even in case we execute thread using runnable interface ?

Ans. Thread class holds the definition of start method ( This is the method that starts execution of new thread and then calls run method within the scope of new thread ). Interfaces don't hold any definition and so does runnable. So it makes it necessary the usage of Thread class , whatever implementation you choose.

When your class extends the thread class, it carries the definition of start method from parent Thread class onto itself and hence new yourClass.start() helps starting a new thread and then executing run method in that new thread scope.

When you implement runnable interface , you are just making it sure to the JVM that you have implemented the required method ( run() ) which the Thread start method will look for upon executing start method.

Q25. Name few classes that implement Runnable interface?

Ans.[TimerTask, FutureTask, ForkJoinWorkerThread, SwingWorker, RenderableImageProducer]

Q26. Runnable is a / an ...

 a.Abstract Class
 b.Concrete Class
 c.Interface
 d.Package

Ans.Interface