简体   繁体   English

与 Eager 相比,使用延迟初始化有什么好处吗?

[英]Is there any benefit of using lazy initialization as compared to Eager?

When to use lazy initialization and when Eager as even if we do lazy then the variable which we will use will still contain some data associated with the following task.何时使用延迟初始化以及何时使用 Eager,即使我们使用延迟,我们将使用的变量仍将包含与以下任务相关的一些数据。 So how do we choose between both?那么我们如何在两者之间进行选择呢? Are there any performance and memory efficient differences between the both?两者在性能和 memory 效率上有什么区别吗?

Please explain any best use case as some threads are supporting eager and some lazy.请解释任何最佳用例,因为一些线程支持渴望和一些懒惰。

In general, the advantages of lazy loading are:一般来说,延迟加载的优点是:

  • If you never need the value, you don't pay any speed or memory penalty in loading or storing it.如果您从不需要该值,则在加载或存储它时无需支付任何速度或 memory 罚款。
  • Start-up is faster.启动速度更快。

And the disadvantages are:缺点是:

  • The first time you need the value, you have to wait while it's loaded.第一次需要该值时,您必须等待它加载。
  • There's often a small overhead associated with accessing the field in a thread-safe way.以线程安全的方式访问字段通常会产生少量开销。 (This is needed because you don't usually want two different threads doing the loading twice; and in any case you don't want any thread to see partly-loaded data.) (这是必需的,因为您通常不希望两个不同的线程执行两次加载;并且在任何情况下,您都不希望任何线程看到部分加载的数据。)

In other languages another disadvantage is extra code to implement that last bit, but Kotlin's by lazy makes it ridiculously easy!在其他语言中,另一个缺点是实现最后一点的额外代码,但 Kotlin 的by lazy让它变得非常容易!

Obviously the overall benefit depends on how likely you are to need the value at all, and the impact of a delay at start-up compared to later on.显然,总体收益取决于您需要该价值的可能性,以及启动延迟与以后相比的影响。

The first thing that we should discuss here is what lazy loading and eager loading are:我们应该在这里讨论的第一件事是什么是延迟加载和急切加载:

Eager Loading is a design pattern in which data initialization occurs on the spot Lazy Loading is a design pattern which is used to defer initialization of an object as long as it's possible Let's see how this actually works with some examples:急切加载是一种设计模式,其中数据初始化发生在现场延迟加载是一种设计模式,用于尽可能推迟 object 的初始化让我们通过一些示例来看看这实际上是如何工作的:

The UserLazy class: UserLazy class:

@Entity
@Table(name = "USER")
public class UserLazy implements Serializable {
 
    @Id
    @GeneratedValue
    @Column(name = "USER_ID")
    private Long userId;
 
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
    private Set<OrderDetail> orderDetail = new HashSet();
 
    // standard setters and getters
    // also override equals and hashcode
 
}

The OrderDetail class: OrderDetail class:

@Entity
@Table (name = "USER_ORDER")
public class OrderDetail implements Serializable {
    
    @Id
    @GeneratedValue
    @Column(name="ORDER_ID")
    private Long orderId;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="USER_ID")
    private UserLazy user;
 
    // standard setters and getters
    // also override equals and hashcode
 
}

One User can have multiple OrderDetails.一个用户可以有多个 OrderDetails。 In eager loading strategy, if we load the User data, it will also load up all orders associated with it and will store it in a memory.在急切加载策略中,如果我们加载用户数据,它还将加载与其关联的所有订单并将其存储在 memory 中。

But, when lazy loading is enabled, if we pull up a UserLazy, OrderDetail data won't be initialized and loaded into a memory until an explicit call is made to it.但是,当启用延迟加载时,如果我们拉起 UserLazy,OrderDetail 数据将不会被初始化并加载到 memory 中,直到对其进行显式调用。

In the next section we will see how the above example is implemented in Hibernate.在下一节中,我们将了解如何在 Hibernate 中实现上述示例。

Find the article here: https://www.baeldung.com/hibernate-lazy-eager-loading在这里找到文章: https://www.baeldung.com/hibernate-lazy-eager-loading

And read this also: https://www.imperva.com/learn/performance/lazy-loading/并阅读此内容: https://www.imperva.com/learn/performance/lazy-loading/

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

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