简体   繁体   English

在不同的Java类中使用已经初始化的类,

[英]Using an already initialized class in different java classes,

I'm new to Java and I'm constructing a springboot application where different classes require data from the same source. 我是Java的新手,正在构建一个springboot应用程序,其中不同的类需要来自同一源的数据。 The source is a couple of files, but the important thing is that it takes quite some time to get the data out; 源是几个文件,但重要的是要花费大量时间才能取出数据。 getting all the data out is about as fast as getting each piece that the different classes require out, so partitioning the function doesn't help. 取出所有数据的速度与取出不同类需要的每个数据的速度差不多,因此对函数进行分区无济于事。

Therefore I'd like to have a class that is initialized once, gets all the data, and then serves the classes that require the data. 因此,我希望有一个初始化一次的类,获取所有数据,然后提供需要数据的类。 Ideally it would be initialized only if requested, and the data would then be saved in the instance. 理想情况下,仅在请求时才将其初始化,然后将数据保存在实例中。

Let's say I have the class: 假设我有课:

package myapp;
import java.util.concurrent.TimeUnit;
import java.lang.InterruptedException;

public class ExampleClass {
    private int usefulValue;

    public ExampleClass(){
        this.usefulValue = slowMethod();
    }

    private int slowMethod(){ //just an example of something that takes time
        int usefulValue;
        try {
            TimeUnit.SECONDS.sleep(500);
        } catch (InterruptedException e){
            ;
        }
        usefulValue = 15;
        return usefulValue;

    }
    public int getUsefulValue(){
        return this.usefulValue;
    }
}

How can I get that to run and usefulValue to be available to the other classes in the package without reloading it in each separate class? 我如何才能usefulValue运行并在包中的其他类usefulValue的值可用,而无需在每个单独的类中重新加载它?

The values are extremely manageable memory-wise and I'm looking specifically for an in-memory solution; 这些值在内存方面非常易于管理,我正在寻找内存中的解决方案。 I could just write it to a file/db or have a socket-server running that serves the application but the question is relating to what's possible to do in Java. 我可以将其写入文件/ db或运行为该应用程序提供服务的套接字服务器,但是问题与Java可以做什么有关。

Ps. PS。 usefulValue changes about once per day usefulValue每天大约变化一次

package myapp;
import java.util.concurrent.TimeUnit;
import java.lang.InterruptedException;

public class ExampleClass {
public static int usefulValue;

public ExampleClass(){
    ExampleClass.usefulValue = slowMethod();
}

private int slowMethod(){ //just an example of something that takes time
    int usefulValue;
    try {
        TimeUnit.SECONDS.sleep(500);
    } catch (InterruptedException e){
        ;
    }
    usefulValue = 15;
    return usefulValue;

}
public int getUsefulValue(){
    return ExampleClass.usefulValue;
}
}

Also in any other class you can use its value like this ExampleClass.usefulValue so its value remains same through out the appilcation or in this thread. 同样,在任何其他类中,也可以使用其值,例如ExampleClass.usefulValue因此在整个应用程序或此线程中,其值保持不变。

You can use lazy initialization. 您可以使用延迟初始化。

    private Integer usefulValue;
    public ExampleClass(){
        // this.usefulValue = slowMethod();
    }

    public int getUsefulValue(){
        if (this.usefulValue == null) {
            this.usefulValue = slowMethod();
        }
        return this.usefulValue;
    }

or use for this: lombok 或用于此目的: 龙目岛

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

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