Java Threading Interview Questions and Answers for Experienced - 1

Question: 1

What is semaphore?

Semaphore is an object which helps two threads to communicate with one another in order to synchronize there operation.

Question: 2

What are daemon threads?

Daemon threads are designed run in the background. One example of a daemon thread is the garbage collector thread. You can use setDaemon () to mark a thread as daemon.

Question: 3

What is the difference between thread and process?

A thread is a path of execution that run on CPU, a process is a collection of threads that share the same virtual memory. A process has at least one thread of execution and a thread always run in a process context.

A process is a collection of virtual memory space, code, data and system resources.

A thread is code that is to be serially executed within a process.

A processor executes threads, not processes.

One more significant difference between process and thread is every process has its own data memory location but all related threads can share same data memory and have their own individual stacks. Thread is a light weighted process, collection of threads become process.

Question: 4

How do we create threads?

There are two ways to create a thread.

extend the java.lang.Thread clas:

Create a class which extends the thread class. When the class is instantiated, the thread and object are created together and the object is automatically bound to the thread. By calling the object’s start() method, the thread is started and immediately calls the object’s run() method.

Implement the java.lang.Runnable interface:

Create a thread and supply it an object with a run() method. This object will be permanently associated with the thread. The object’s run method will be invoked when the thread is started. This method of thread creation is useful if you want many threads sharing an object.

Question: 5

What is thread safety and synchronization?

Thread safe is used to describe that can run safely in multithreaded environments allowing accessing to data in a safe and efficient way.

Thread safety is achieved by synchronization .

Synchronization assures that the object data is not accessed by multiple threads at the same time.

Error Report!

Related Questions