Why does the Object class have Thread methods?

Why does the Object class have stream-related methods like wait() , notify() , notifyAll() ?

We need to extend Thread or implement Runnable to give a class like Thread-like. So why weren't they part of any Thread or Runnable object?

+4
source share
2 answers

Java concurrency uses locks to provide mutually exclusive access to objects in a multi-threaded environment, and locks are associated with each object in Java (such as "Object"), and not just Threads.

1) Waiting and notification is a communication mechanism between two threads in Java. And the Object class is the right place to make them available for each object, since it is a superclass of all objects.

2) Locks are available for each object, which is another reason for waiting and notification in the Object class, and not in the Thread class.

+4
source

If a thread is blocked on an instance of the object, calling a notification about this instance of the object will wake these threads. Thus, since a lock is an instance of an object, the operations associated with this lock apply to the instance of the object.

0
source

Source: https://habr.com/ru/post/1479875/


All Articles