Saturday, April 8, 2017

Difference between Spring Container and Servlet Container?

Spring container is responsible for creating the objects, wiring them together, configuring them, and managing their complete lifecycle from creation till destruction. The Spring container uses dependency injection (DI) to manage the components that make up an application. These objects are called Spring Beans.
While Servlet container is basically web server or application server one who is responsible for creating and maintaining life cycle of a servlet like init, service, destroy in a nut shell. It has other functionalities as well in addition to maintaining lifecycle of a servlet.

Friday, April 7, 2017

Review of java.io Class Properties

A class with the word InputStream or OutputStream in its name is used for reading or
writing binary data, respectively.
■■ A class with the word Reader or Writer in its name is used for reading or writing
character or string data, respectively.
■■ Most, but not all, input classes have a corresponding output class.
■■ A low-level stream connects directly with the source of the data.
■■ A high-level stream is built on top of another stream using wrapping.
■■ A class with Buffered in its name reads or writes data in groups of bytes or characters
and often improves performance in sequential file systems.

Differences between Streams and Readers/Writers

1. The stream classes are used for inputting and outputting all types of binary or byte
data.
2. The reader and writer classes are used for inputting and outputting only character and
String data.

Difference between absolute path and relative path?

The absolute path of a file or directory is the full path from the root directory to the file or directory, including all subdirectories that contain the file or directory.

Alternatively, the relative path of a file or directory is the path from the current working directory to file or directory.

What is race condition?

A race condition is an undesirable result that occurs when two tasks, which should be completed sequencially are completed at the same time.

What is Starvation and Livelock in Multithreading Java?

Starvation:

Starvation occurs when a single thread is perpetually access to a shared resource or lock.
The thread is active and unable to complete its work as a result of other threads constantly taking the resource that they trying to access.

Livelock:

Livelock occurs when two or more threads are conceptually blocked forever, although they
are each still active and trying to complete their task. Livelock is a special case of resource
starvation in which two or more threads actively try to acquire a set of locks, are unable to
do so, and restart part of the process.

Livelock is often a result of two threads trying to resolve a deadlock.

Why use a concurrent collection classes in java?

Concurrent collection classes is extremely convenient to use it without using Thread class.

Advantages:

 1. Prevents mistake from own custom implementation such as if we forgot to synchronize one of the accessor methods.
2. Concurrent collections avoid unnecessary synchronizations.
3. The concurrent classes were created to help avoid common issues in which multiple
threads are adding and removing objects from the same collections

The following code program is alternate version of our implementation that does not use the synchronized key word.

public class ConcurrentExample{

 private Map<String, String> map = new ConcurrentHashMap<>();

}