Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Secure Methods  (Read 1229 times)
0 Members and 1 Guest are viewing this topic.
Offline Pyrodragoon

Junior Member




Art calculated with java...


« Posted 2008-04-02 12:17:04 »

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 ?
Offline erikd

JGO Ninja


Medals: 15
Projects: 4
Exp: 14 years


Maximumisness


« Reply #1 - Posted 2008-04-02 13:08:14 »

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?

Offline Pyrodragoon

Junior Member




Art calculated with java...


« Reply #2 - Posted 2008-04-02 14:21:08 »

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.  Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Wildern

Junior Member





« Reply #3 - Posted 2008-04-02 14:28:23 »

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;
Offline Abuse

JGO Coder


Medals: 10


falling into the abyss of reality


« Reply #4 - Posted 2008-04-02 14:49:21 »

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.  Smiley

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.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline cylab

JGO Knight


Medals: 34



« Reply #5 - Posted 2008-04-02 15:01:51 »

If this in fact is not possible in java I will test the problematic programs again and hopefully find the real
reason.

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)

Mathias - I Know What [you] Did Last Summer!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (31 views)
2014-04-15 18:08:23

BurntPizza (28 views)
2014-04-15 03:46:01

UprightPath (43 views)
2014-04-14 17:39:50

UprightPath (26 views)
2014-04-14 17:35:47

Porlus (42 views)
2014-04-14 15:48:38

tom_mai78101 (64 views)
2014-04-10 04:04:31

BurntPizza (124 views)
2014-04-08 23:06:04

tom_mai78101 (224 views)
2014-04-05 13:34:39

trollwarrior1 (190 views)
2014-04-04 12:06:45

CJLetsGame (198 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!