10. Multi Threading Questions

What is multithreading?
Multithreading is a process of executing multiple threads simultaneously. Its main advantage is:
  • Threads share the same address space.
  • Thread is lightweight.
  • Cost of communication between process is low.


What is thread?

A thread is a lightweight subprocess.It is a separate path of execution.It is called separate path of execution because each thread runs in a separate stack frame.


What does join() method?

The join() method waits for a thread to die. In other words, it causes the currently running threads to stop executing until the thread it joins with completes its task.


What is difference between wait() and sleep() method?

                             wait()
                                  sleep()
1) The wait() method is defined in Object class.
The sleep() method is defined in Thread class.
2) wait() method releases the lock.
The sleep() method doesn't releases the lock.


Is it possible to start a thread twice?

No, there is no possibility to start a thread twice. If we does, it throws an exception


Can we call the run() method instead of start()?

yes, but it will not work as a thread rather it will work as a normal object so there will not be context-switching between the threads.

What is synchronization?
Synchronization is the capabilility of control the access of multiple threads to any shared resource.It is used:
  1. To prevent thread interference.
  2. To prevent consistency problem.

What is the purpose of Synchronized block?
  • Synchronized block is used to lock an object for any shared resource.
  • Scope of synchronized block is smaller than the method.


What is static synchronization?

If you make any static method as synchronized, the lock will be on the class not on object


What is the difference between notify() and notifyAll()?

The notify() is used to unblock one waiting thread whereas notifyAll() method is used to unblock all the threads in waiting state.


What is deadlock?

Deadlock is a situation when two threads are waiting on each other to release a resource. Each thread waiting for a resource which is held by the other waiting thread.


No comments:

Post a Comment