简体   繁体   English

为什么wait,notify和notifyAll是Object类中的方法,不像Cloneable

[英]Why wait, notify and notifyAll are methods in Object class not like Cloneable

Recently, I was asked in interview why wait , notify , and notifyAll are used. 最近,我在面试时被问到为什么要使用waitnotifynotifyAll I explained them. 我解释了他们。

After that they asked me to assume an application is always single threaded. 之后,他们让我假设一个应用程序始终是单线程的。 Is it really required? 真的需要吗? My answer was no. 我的回答是否定的。

Then, they asked why is design like wait , notify , and notifyAll are methods on the Object class. 然后,他们问为什么设计像waitnotifynotifyAllObject类的方法。 Why doesn't Java have an interface and these methods are in that interface and which ever class wants to implement it can use it. 为什么Java没有接口,并且这些方法在该接口中,并且哪个类想要实现它可以使用它。 So, I was kind of stuck and unable to think over this design. 所以,我有点卡住,无法思考这个设计。 Can anyone please sow light over this? 任何人都可以为此播种吗?

JVM uses OS-level threads. JVM使用OS级别的线程。 That means that each concrete JVM for each concrete OS handles threads differently. 这意味着每个具体操作系统的每个具体JVM都以不同方式处理线程。 And these methods are not only implemented in Object class, they are marked as native , which kind of means that the are implemented in system layer of JVM. 而且这些方法不仅在Object类中实现,它们被标记为native ,这意味着它们在JVM的系统层中实现。

And if those methods were in some interface, that would mean that anybody can redefine them. 如果这些方法在某个界面中,那就意味着任何人都可以重新定义它们。

Wait and notify and notifyAll is not just normal methods or synchronization utility, more than that they are communication mechanism between two threads in Java. 等待并通知和notifyAll不仅仅是普通方法或同步实用程序,更重要的是它们是Java中两个线程之间的通信机制。 And Object class is correct place to make them available for every object if this mechanism is not available via any java keyword like synchronized. 如果通过任何java关键字(例如synchronized)无法使用此机制,那么Object类是使它们可用于每个对象的正确位置。 Remember synchronized and wait notify are two different area and don't confuse that they are same or related. 记住同步和等待通知是两个不同的区域,不要混淆它们是相同的或相关的。 Synchronized is to provide mutual exclusion and ensuring thread safety of Java class like race condition while wait and notify are communication mechanism between two thread. 同步是提供互斥并确保Java类的线程安全,如等待和通知是两个线程之间的通信机制。

Then, they asked why is design like wait , notify , and notifyAll are methods on the Object class. 然后,他们问为什么设计像waitnotifynotifyAllObject类的方法。 Why doesn't Java have an interface and these methods are in that interface and which ever class wants to implement it can use it. 为什么Java没有接口,并且这些方法在该接口中,并且哪个类想要实现它可以使用它。

All of these methods are implemented in native code and they integrate closely with the synchronized block that wraps them. 所有这些方法都在本机代码中实现,并且它们与包装它们的synchronized块紧密集成。 They are part of the Java language definition and have specific behaviors that programmers rely on. 它们是Java语言定义的一部分,具有程序员所依赖的特定行为。 It would not be appropriate for them just to be interface methods that any object would implement. 它们不适合作为任何对象实现的接口方法。

When one object calls obj.wait(); 当一个对象调用obj.wait(); on another object, it doesn't have to worry about the implementation of wait . 在另一个对象上,它不必担心执行wait It needs to make sure that it has a mutex lock on that object so it can make critical updates to it or other storage and if the wait method was implemented by the object itself, then that object could violate the language requirements and, for example, allow multiple threads into the protected block at the same time. 它需要确保它对该对象具有互斥锁,以便它可以对它或其他存储进行关键更新,如果wait方法是由对象本身实现的,那么该对象可能违反语言要求,例如,允许多个线程同时进入受保护的块。 A thread can synchronize and call wait / notify / notifyAll on another object or not without having to worry about whether or not that object has implemented those methods appropriately. 线程可以synchronize并在另一个对象上调用wait / notify / notifyAll ,而不必担心该对象是否已正确实现这些方法。 By making them final methods on Object the behavior will work the same regardless of the object type or local implementation. 通过在Object上创建final方法,无论对象类型或本地实现如何,行为都将起作用。

Also, as I mentioned, wait / notify / notifyAll are integrated closely with the surrounding synchronized block. 另外,正如我所提到的, wait / notify / notifyAll与周围的synchronized块紧密集成。 When a thread is blocked in wait() the surrounding synchronized lock is released so that other threads can get access to the protected block. 当一个线程在wait()被阻塞时,周围的synchronized锁被释放,以便其他线程可以访问受保护的块。 This coordination would not be possible if the wait() was just a simple method call without other strange language features. 如果wait()只是一个没有其他奇怪语言功能的简单方法调用,那么这种协调是不可能的。

This reminds me of my other answer here: Concept behind putting wait(),notify() methods in Object class 这让我想起了我的另一个答案: 在Object类中放置wait(),notify()方法的概念

It was a design goal from the start that Java programs would be multithreaded. 从一开始就是Java程序将是多线程的设计目标。 Remember the plan was for Java to make embedded programming less intimidating, the whole serverside web application thing (leading to the commoditization of Sun's core business) was an accident. 请记住,Java的计划是使嵌入式编程不那么令人生畏,整个服务器端Web应用程序(导致Sun核心业务的商品化)是一个意外。

Since the goal was to enable creating embedded applications that would talk to other devices, it had to be multithreaded in order to be network-friendly and event-driven. 由于目标是创建可与其他设备通信的嵌入式应用程序,因此必须采用多线程才能实现网络友好和事件驱动。 But writing efficient multithreaded servers wasn't high on the list for java. 但是编写高效的多线程服务器在java的列表中并不高。

Java didn't have ReentrantLock or nonblocking i/o for a long time. Java长期没有ReentrantLock或非阻塞i / o。 Initially the main data structures available were Vector, Hashtable, and StringBuffer (all of which had synchronized on all public methods). 最初可用的主要数据结构是Vector,Hashtable和StringBuffer(所有这些都已在所有公共方法上同步)。 From that choice it seems like the goal was good-enough, as opposed to being as efficient as possible. 从这个选择来看,似乎目标是足够好的,而不是尽可能高效。 Later on it was clear Java needed to be more efficient for the use case of server applications and 1.2 introduced equivalents of Vector and Hashtable that were unsynchronized. 后来很明显,Java需要对服务器应用程序的使用情况更有效,并且1.2引入了不同步的Vector和Hashtable的等价物。 This seemed like an afterthought, a course adjustment made once it was apparent Java had a new role it wasn't previously designed for. 这看起来像是一个事后的想法,一旦显然Java有一个以前没有设计过的新角色,就会进行课程调整。

If Java had stayed in the niche it was created for then possibly intrinsic locks might have been adequate. 如果Java留在了利基市场,那么它可能就已经足够了。 It seems the initial plan was for intrinsic locks only, so that the lock might as well be wired into the Object. 似乎最初的计划仅针对内部锁,因此锁也可以连接到Object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM