2

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

BrainCrash
  • 12,992
  • 3
  • 32
  • 38
  • 1
    Assuming a single thread, "As I understand there are chances (especially if the method setX() does more than just set x ) that elaborateX() will return "1"." is incorrect. – Anthony Grist Feb 20 '12 at 17:02
  • Yes, it will. In a single-threaded application like the one you have above, you can expect 6 to be returned because each instruction is fully executed before the next. It is deterministic. – jbranchaud Feb 20 '12 at 17:02

6 Answers6

2

Yes.

Ordinary Java code is always synchronous.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

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

mikera
  • 105,238
  • 25
  • 256
  • 415
1

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.

Community
  • 1
  • 1
biziclop
  • 48,926
  • 12
  • 77
  • 104
0

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

Sly
  • 2,105
  • 5
  • 22
  • 28
0

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.

jbranchaud
  • 5,909
  • 9
  • 45
  • 70
0

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

Cratylus
  • 52,998
  • 69
  • 209
  • 339