11. What is the difference between processes and threads ?
A process is an execution of a program, while aThread is
a single execution sequence within a process. A process can contain multiple
threads. A Thread is
sometimes called a lightweight process.
12. Explain different ways of creating a thread. Which one would
you prefer and why ?
There are three ways that can be used in order for a Thread to
be created:
·
A class may extend the Thread class.
·
A class may implement the Runnable interface.
·
An application can use the Executor framework,
in order to create a thread pool.
The Runnable interface
is preferred, as it does not require an object to inherit the Thread class.
In case your application design requires multiple inheritance, only interfaces
can help you. Also, the thread pool is very efficient and can be implemented
and used very easily.
13. Explain the available thread states in a high-level.
During its execution, a thread can reside in one of the
following states:
·
NEW:
The thread becomes ready to run, but does not necessarily start running
immediately.
·
RUNNABLE:
The Java Virtual Machine (JVM) is actively executing the thread’s code.
·
BLOCKED:
The thread is in a blocked state while waiting for a monitor lock.
·
WAITING:
The thread waits for another thread to perform a particular action.
·
TIMED_WAITING:
The thread waits for another thread to perform a particular action up to a
specified waiting time.
·
TERMINATED:
The thread has finished its execution.
14. What is the difference between a synchronized method and a
synchronized block ?
In Java programming, each object has a lock. A thread can
acquire the lock for an object by using the synchronized keyword. The
synchronized keyword can be applied in a method level (coarse grained lock) or
block level of code (fine grained lock).
15. How does thread synchronization occurs inside a monitor ?
What levels of synchronization can you apply ?
The JVM uses locks in conjunction with monitors. A monitor is
basically a guardian that watches over a sequence of synchronized code and
ensuring that only one thread at a time executes a synchronized piece of code.
Each monitor is associated with an object reference. The thread is not allowed
to execute the code until it obtains the lock.
16. What’s 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.
17. How do you ensure that N threads can access N resources
without deadlock ?
A very simple way to avoid deadlock while using N threads is to
impose an ordering on the locks and force each thread to follow that ordering.
Thus, if all threads lock and unlock the mutexes in the same order, no
deadlocks can arise.
Java
Collections
18. What are the basic interfaces of Java Collections Framework
?
Java Collections Framework provides
a well designed set of interfaces and classes that support operations on a
collections of objects. The most basic interfaces that reside in the Java
Collections Framework are:
·
Collection,
which represents a group of objects known as its elements.
·
Set, which is a collection
that cannot contain duplicate elements.
·
List,
which is an ordered collection and can contain duplicate elements.
·
Map, which is an object
that maps keys to values and cannot contain duplicate keys.
19. Why Collection doesn’t extend Cloneable and Serializable
interfaces?
The Collection interface
specifies groups of objects known as elements. Each concrete implementation of
a Collection can
choose its own way of how to maintain and order its elements. Some collections
allow duplicate keys, while some other collections don’t. The semantics and the
implications of either cloning or serialization come into play when dealing
with actual implementations. Thus, the concrete implementations of collections
should decide how they can be cloned or serialized.
20. What is an Iterator ?
The Iterator interface
provides a number of methods that are able to iterate over anyCollection.
Each Java Collection contains
the iterator method
that returns an Iterator instance.
Iterators arecapable of removing elements from the
underlying collection during the iteration.
No comments:
Post a Comment