Calling in the constructor a method outside with the parameter this of the object/constructor

老子叫甜甜 提交于 2019-12-25 00:34:25

问题


hey i saw this topic here: Can I call methods in constructor in Java?

My Question is smilar, but a little bit different.

I want to know whats happening, if i call from a constructor from class A something outside of that from class B with this from A. In my example below A is JobImpl, and B is that queue in Scheduler.

class JobImpl implements Job
{       

    public JobImpl(Scheduler s, JobHandler runJob, boolean advancedWaitOnCancel, boolean oneShot)
    {           
        this.s = s;         
        this.runJob = runJob;
        this.advancedWaitOnCancel = advancedWaitOnCancel;
        this.oneShot = oneShot;     

        jobReadyPhaser = advancedWaitOnCancel ? new AdvancedCountdownImpl(this) : new AdvancedCountdownNoOp();

        this.s.jobs.add(this);          
    }

}

In my example i call two foreign methods/other constructors.

I call the constructor of AdvancedCountdownImpl(this) and i add this to a queue outside of that class.

I assign the input-parameter of that constructor to final fields. I could move that this.s.jobs.add(this) a little bit up, before the asssignments of that final fields; that would be no problem - from the compiler side.

In that linked topic, i saw that there could be inheritance problems.

From my class JobImpl, there exists a few subclasses (Not only from my "interface", but really from JobImpl ^^)

The interessting thing is the jobs.add(this). It could be, that - before the constructor ends - a other thread access that list and do something with their content. So that thread could access that Job and do something with that.

If i move in that constructor that row with jobs.add(this) a little bit up, after this.s = s, then it could be, that the other final fields are not instantiated. Is that correct?

In that case, what would my programm do - then?

Ok now i placed that this.s.jobs.add(this) to the end of that constructor. So i guess, the final fields would be instantiated

Or not? Maybe the compiler swaps intern the rows for optimization purposes?

Ok, if the compiler wouldn'd do that, is then my JobImpl at ANY! time the other thread access over the queue my Job completely instanciated?

Or would i still have in that case any inheritance problems?, if that other thread would access some methods, who are overwritten?

Shouldn't i push that jobs to that queue in the constructor, but in a secound step? So constructor-call after that a init-call?

Is there a better solution?

When i can do that, what i have done? - push this from the constructor to the outside? Maybe if there is no other thread?

My jobReadyPhaser is a final field too. So i must construct that AdvancedCountdownImpl(this) in the constructor.

But i guess here i'am 100% safe, cause the AdvancedCountdownImpl dont do something else with that this, but saving it to its own field, for later use.

来源:https://stackoverflow.com/questions/58314299/calling-in-the-constructor-a-method-outside-with-the-parameter-this-of-the-objec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!