长时间运行的构造函数会创建半初始化的对象吗?

发布于 2024-12-01 03:02:36 字数 609 浏览 0 评论 0原文

我有一个“A”类,它读取 XML 文件并进行一些处理。我在构造函数中放置了一个方法“load”,但我想知道如果 XML 文件很大并且加载需要时间会发生什么。

class A
{
    public String fileName;

    A(String fileName)
    {
        this.fileName = fileName;
        load();
    }

    private load()
    {
        //here i load some xml file by given file name;
    }

    public searchByTag(String sometag)
    {
        //some search
    }

    public extractData()
    {
        //extract some data
    }
}  

例如,如果我们有以下场景:

A a = new A("somefile");
a.searchByTag("tag");
a.extractData();

对象“a”是在文件加载后立即创建的,对吗?

I have a class "A" which reads an XML file and does some processing. I put a method "load" in the constructor, but I'm wondering what happens if the XML file size is large and it takes time to be loaded.

class A
{
    public String fileName;

    A(String fileName)
    {
        this.fileName = fileName;
        load();
    }

    private load()
    {
        //here i load some xml file by given file name;
    }

    public searchByTag(String sometag)
    {
        //some search
    }

    public extractData()
    {
        //extract some data
    }
}  

For example if we have the following scenario:

A a = new A("somefile");
a.searchByTag("tag");
a.extractData();

The object "a" is created just after file is loaded, right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

披肩女神 2024-12-08 03:02:36

是的,执行该代码段的线程将在返回 A 的实例之前完成所有加载。

从技术上讲,对象“a”是在加载之前创建的(在加载中您可以安全地引用它),但它被分配给仅当构造函数返回时才使用变量“a”,这意味着当它也完成执行 load() 方法时。

Yes, the thread executing that piece of code will go thru all the load before returning the instance of A.

Technically, the object "a" is created before the load (inside load you can safely refer to this), but it is assigned to the variable "a" only when the constructor return, which means when it has finished executing also the load() method.

作死小能手 2024-12-08 03:02:36

由于 load() 是从构造函数中调用的,因此实例构造将花费与解析 XML 文件所需的时间一样多的时间。构造函数仅在完成时退出,即对象准备就绪。在您的情况下,仅当 XML 已被解析时。

Since load() is called from constructor the instance construction will take as much time as it is needed to parse XML file. The constructor exits only when it is done, i.e. the object is ready. In your case only when the XML has been parsed.

朦胧时间 2024-12-08 03:02:36

程序的控制流在所有代码执行完毕之前不会返回到构造函数调用,除非发生错误,在这种情况下将引发异常。

如果没有发生错误,则如您所述,将在加载文件后创建对象。

The control flow of the program doesn't return to the constructur call until all the code has been executed, except if errors happened, in which case an exception would be thrown.

If no errors happened the object would be created after the file is loaded, as you state.

我是有多爱你 2024-12-08 03:02:36

一旦您调用构造函数,该对象就会被创建,但除非 load 方法返回,否则该对象不会返回给您。

但问题是基本设计不正确。您不应该从构造函数中调用 load 方法。

让构造函数只调用该对象,然后调用该对象的 load 方法来读取 xml 文件。您需要在类中重载读取方法,如下所示:

private load()
{
//here i load some xml file by given file name;
}

//for loading from default location
private load(String filePath)
{
//here i load some xml file by given file name;
}


private load(File file)
{
//here i load some xml file by given file name;
}

您明白了!然后您的调用程序将可以灵活地针对不同情况调用不同的加载方法。

您还应该有两到三种不同的方法来首先获取文件大小,然后调用相应的方法。

  Have a look at this thread http://stackoverflow.com/questions/116574/java-get-file-size-efficiently  to get a better idea about an efficient way to deduce file size.

The object will be created as soon as you have called the constructor but it won't be returned to you unless the load method has returned .

But the issue is the basic design is not right. You should not call the load method from the constructor .

Let the constructor just call the object and then call a load method on it to read the xml file. You need to have over loaded read methods in the class like this :

private load()
{
//here i load some xml file by given file name;
}

//for loading from default location
private load(String filePath)
{
//here i load some xml file by given file name;
}


private load(File file)
{
//here i load some xml file by given file name;
}

You get the idea !!. And then your calling program will have the flexibility of invoking the load method differently for different situations .

You should also have two-three different methods for first getting the file size and then claling a corresponding method.

  Have a look at this thread http://stackoverflow.com/questions/116574/java-get-file-size-efficiently  to get a better idea about an efficient way to deduce file size.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文