Thursday, November 14, 2019

Can We Use Wait And Notify Without Synchronized

Q- Can we use wait and notify without synchronized? Or Why wait and notify method must be called in synchronized context?
  Answer:-    No,

Q- Can we override wait () and notify () methods?

Q- What if Notify is called before wait?

Q- Why wait and notify is declared in object class?
  Answer:-  wait(), notify() and notifyAll() are not normal methods that used for inter-thread communication.
wait(), notify() and notifyAll() are used on object not on thread.
There is shared object between the threads that acts as a communicator among the threads.

Threads lock an object, wait on an object and notify an object.
But threads themselves not have knowledge of each others status.

Q- Does notify Release lock?
  Answer:-     By Calling notify() method on an object releases the lock of that object. But its depends upon a thread calls notify() method on an object, the thread may not release the lock of that object immediately, as the thread may have some more task or jobs to do.

Q- Can a thread enter a synchronized block without acquiring a lock?
  Answer:-  No,

Q- Does wait method release lock?
Yes, Because, Wait() method is an instance method that is used for thread synchronization. But it can only be called from a synchronized block or synchronized method . While the wait() method is called on object then that thread is goes in to waiting state and It releases the lock on the object so that another thread can jump in and acquire a lock.

No comments:

Post a Comment