0

I have a Java program which create 2 threads, inside these 2 threads, they are trying to update the global variable abc to different value, let's say integer 1 and integer 3.

Let's say they execute the code at the same time (at same milisecond), for example:

public class MyThread implements Runnable{
    public void run(){
        while(true){
            if (currentTime == specificTime){
                abc = 1; //another thread update abc to 3
            }
        }
    }
} 

In this case, how can we determine the result of the variable abc? I am very curious how Operating System schedule the execution?

(I know Synchronize should be used, but I just want to know naturally how the system will handle this kind of conflict problem.)

2
  • You should read up on locking. msdn.microsoft.com/en-us/magazine/cc163744.aspx Commented Sep 21, 2013 at 12:42
  • 4
    @Aubin writing and reading primitives in java IS atomic, only long and double are not atomic. His problem is that reading and then writing is no atomic operation. Commented Sep 21, 2013 at 12:49

6 Answers 6

3

The operating system has little involvement in this: at the time your threads are running, the memory allocated to abc is under control of JVM running your program, so it's your program that is in control.

When two threads access the same memory location, the last writer wins. Which particular thread gets to be the last writer, however, is non-deterministic, unless you use synchronization.

Moreover, without you taking special care of accessing the shared data, one thread may not even see the results of the other thread writing to the abc location.

To avoid synchronization issues, you should use synchronization or one of the java.util.concurrent.atomic classes.

Sign up to request clarification or add additional context in comments.

Comments

3

From Java's perspective the situation is fairly simple if abc is not volatile or accessed with appropriate synchronisation.

Let's assume that abc is 0 originally. After your two threads have updated it to respectively 1 and 3, abc could be observed in three states: 0, 1 or 3. Which value you get is not deterministic and the result may vary from one run to the other.

Comments

2

Depends on the operating system, running environment etc.

Some environments will actually stop you from doing this - known as thread safety.

Otherwise the results are totally unpredictable which is why it is so dangerous to do this.

It mainly just depends on which thread updated it last for what the value will be. One thread will get CPU cycles before the other to do the atomic operation first.

Also, I don't think that operating systems go as far as to schedule threads because in most operating systems it is the program that is responsible for them, and without explicit calls like synchronise, or a threading pool model then I think the order of execution is pretty hard to predict. Its a very environment dependent thing.

Comments

1

From the system's perspective the result will depend on many software, hardware and run-time factors that cannot be known in advance. From this perspective there is no conflict nor a problem.

From the programmer's perspective the result is not deterministic and therefore a problem/conflic. The conflict needs to be resolved at design-time.

Comments

1

In this case, how can we determine the result of the variable abc? I am very curious how Operating System schedule the execution?

The result will not be deterministic, as the value will be the last written one. You can not make any guarantee about the result. The execution is scheduled like any other one. As you demand no synchronization in your code the JVM will not enforce anything for you.

I know Synchronize should be used, but I just want to know naturally how the system will handle this kind of conflict problem.

Simple said: it wont, as for the system there is no conflict. Only for you, the programmer, problems will occur, since you will eventually run into a data race and not deterministic behavior. It is completely up to you.

Comments

1

just add volatile modificator to your variable, then it'll be udpated through all threads. And thread reading it will get it's actual value. volatile means that value will be always up to date for all threads accessing it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.