BrainCrash
BrainCrash

Reputation: 13182

Does "if" always wait method to finish?

I'm fairly new to Java and have a doubt that needs to be cleared.

Using code like this:

public int x;

public int elaborateX(){
    x = 0;
    setX();
    x+=1;
    return x;
}

public void setX(){
    //...
    //some workload here
    //...
    x = 5;
}

As I understand there are chances (especially if the method setX() does more than just set x ) that elaborateX() will return "1".

I know after looking the issue around that Threads can be used to prevent the "bug", but my question is; will the following always wait for setX() to be finish executing?

public int x;

public int elaborateX(){
    x = 0;
    if(setX()){
       x+=1;
    };

    return x;
}

public boolean setX(){
    //...
    //some workload here
    //...
    x = 5;
    return true;
}

Will elaborateX() always return "6" in this case?

I'm asking because I need to be 100% sure otherwise I will use the "proper approach" instead of this "trick".

Thanks

Upvotes: 2

Views: 359

Answers (6)

biziclop
biziclop

Reputation: 49804

There is no chance of it ever returning anything other than 6, unless you use multiple threads.

That's what threads are about, every operation is threaded one after the other, there's no reordering of operations. (At least nothing that you can see, the VM itself will do all sorts of crazy optimisations, but it must not be visible.)

However, Java uses short-circuit evaluation of boolean operators || and &&, which although unrelated, can cause similar problems if you're unprepared.

Upvotes: 1

Cratylus
Cratylus

Reputation: 54094

As I understand there are chances (especially if the method setX() does more than just set x ) that elaborateX() will return "1".

This can only happen if you spawn a new thread in setX so your elaborateX execution thread will continue finding x as 1.

Will elaborateX() always return "6" in this case?

No and yes.This depends on your implementation.If you spawn a new thread in the setX then in the elaborateX thread execution you will continue and find x to be 1.

You must implement a mechanism to wait for setX to finish and get the result.

This could be either by timed wait/interruption or by joining the thread

Upvotes: 0

jbranchaud
jbranchaud

Reputation: 6087

You can expect your application to always return 6 (assuming a single-thread program).

An if statement must execute the code inside its conditional block in order for the program to branch correctly. I am not sure where the idea of the conditional not fully executing came from, but I think you can see how it would be problematic for the deterministic execution of the program. I am not even sure how an if statement would branch without the code in its conditional being fully executed/evaluated.

Upvotes: 0

Sly
Sly

Reputation: 2101

No, you have see the "synchronized" in Java and thread programming. Are you already familiar with thread programming in another language?

Upvotes: 0

mikera
mikera

Reputation: 106401

You can run into issues if two different threads are altering variables at the same time.

However, as your examples only appear to have one thread, you can safely assume that everything will happen in order (i.e. the method setX will always finish completely before any subsequent instructions).

Upvotes: 2

SLaks
SLaks

Reputation: 887987

Yes.

Ordinary Java code is always synchronous.

Upvotes: 2

Related Questions