Skip to main content
gamedev.net
🔒 Locked

[java] Execution flow...

Started by kino Apr 13, 2005 at 4:27 PM 3 replies 1k views
Original Post
kino
kino
Suppose that somewhere from a class X there’s a message to an object Y which don’t return anything; Class X { …code code code Y.someMethod(); // someMethod is void type next line } Class Y { …code code code public void someMethod() { While (true) { Do anything forever!!!} } } The class X executes the “next line” or not? I’m afraid to hear a big YES, OFF COURSE!! , but that’s not what’s happening in my program… Thanks all!!
Long live Rock!!
daireq
daireq
I'd be surprised if it WAS happening in your program ! that while loop will execute forever unless theres a break or it throws an exception or something that knocks the execution out of the loop.

If what you want is for X to resume after the call to someMethod, you'll have to create a seperate thread for Y to run in, and start it up. In that case X will resume immediately after its started the thread and both will run concurrently.

D.
CaptainJester
CaptainJester
In other words, the way you have it set up now, Y.someMethod() will not return.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
kino
kino

Oops, sorry! I forget to say that Y already is a thread!
So, next line must be executed, rigth?
Long live Rock!!
pirate_dau
pirate_dau
Even if Y "is a thread", if you call someMethod() from the thread which X is running in, it's going to wait for Y to return before executing the next statement.

Maybe this will explain things a bit...

public class X {    public static void main(String[] args) {        Thread thread = new Thread(new Y());        thread.start();        System.out.println("foo");        new Y().someMethod();        System.out.println("bar");    }}public class Y implements Runnable {    public void run() {        someMethod();    }    public void someMethod() {        while(true) {            // do something        }    }}


"foo" should be printed, but "bar" will not.

EDIT: Fixed source tags
EDIT: Woops, meant thread.start() and not thread.run()

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.