Secure Methods

Hi there :wink:
I´d like to know how to secure that a method is fully done before anything else happens that might
interfere with the methods tasks.
F.e. when I want to load a map from a file and secure that it´s fully loaded before the paint()-method draws the map.
I think sometimes java jumps to the next task before the last one is fully completed.
Let my method be loadMap(); and
the code be

public void doWhatever(){
loadMap();
saveMap();
}

I experienced java trying to start the saveMap() method before fully finishing the loadMap() method
which of course causes some serious problems.

I tried to solve the problem by doing the following:

public void doWhatever(){
if( loadMap() ) { }
saveMap();
}

public void loadMap(){
[…]

return true;
}

I guess with this the method calling loadMap() waits until it receives the return true and then continues its tasks.
I hope this is right, but is there a better way ?

The JVM will never continue with the next statement before the previous has finished.
Are you sure loadMap() doesn’t do stuff in a separate Thread, or that loadMap() doesn’t silently crash somewhere inside (i.e. a try-catch somewhere with an empty catch block)?
Could you post your loadMap() method?

My loadMap() method was just a possible example of how it could
look like in my program, in real it´s a bit more complicated.
If its save to say that java does whatever happens in a method step by step
there must be another explanation why my programs crashed.
I’m sorry I can’t easily post the code because it bothered me in many programs and is
split over several classes.
I can try to find one example but somehow this was just a general issue
that’s been giving me a hard time and seemed to me to be a reason for a crash.
If this in fact is not possible in java I will test the problematic programs again and hopefully find the real
reason.
Thanks for saving me the future trouble of embedding every questionable method in an if-clause. :slight_smile:

It is possible you are running into the issue that the paint happens in a separate thread.
You could have a boolean flag, say initialized that starts out false, and gets set to true when you are finished loading/setting up everything that needs to paint.
Then in your paint method (or any method that is crashing because things are not fully initialized)
if (!initialized) return;

99% of java beginner questions (and a good portion of more expert questions!) could be avoided if more people were familiar with using IDEs & debuggers.

Stick a breakpoint on your loadMap() method, and one on your saveMap() method and you will see exactly when, and from where the methods are being invoked.
If it’s a thrown Exception causing your problems, you could set an exception break point to halt execution at the point the exception is thrown, allowing you to examine exactly what was the cause.

Like the others said, the reason will probably be a Threading issue: Either you call (without knowing) some method that does work asynchronously on a background thread, or you interfere with a method that is called itself from a background Thread (like paint(), which is called by the AWT event Thread)