1

Java's multithreaded code is finally mapped to the operating system thread for execution.

Is the operating system thread not thread safe?

Why use the Java memory model to ensure thread safety?Why define the Java memory model?

I hope someone can answer this question, I have looked up a lot of information on the Internet, still do not understand!

The material on the web is all about atomicity, visibility, orderliness, and using the cache consistency model as an example, but I don't think it really answers the question.

thank you very much!

1
  • 3
    Thread-safety is not a property of threads. It's a property of code that manipulates data that is shared between threads. Commented May 27, 2020 at 6:57

3 Answers 3

1

The operating system thread is not thread safe (that statement does not make a lot of sense, but basically, the operating system does not ensure that the intended atomicity of your code is respected).

The problem is that whether two data items are related and therefore need to be synchronized is only really understood by your application.

For example, imagine you are defining a ListOfIntegers class which contains an int array and count of the number of items used in the array. These two data items are related and the way they are updated needs to be co-ordinated in order to ensure that if the object is accessed by two different threads they are always updated in a consistent manner, even if the threads update them simultaneously. Only your application knows how these data items are related. The operating system doesn't know. They are just two pieces of memory as far as it is concerned. That is why you have to implement the thread safety (by using synchronized or carefully arranging how the fields are updated).

The Java "memory model" is pretty close to the hardware model. There is a stack for primitives and objects are allocated on the heap. Synchronization is provided to allow the programmer to lock access to shared data on the heap. In addition, there are rules that the optimizer must follow so that the opimisations don't defeat the synchronisations put in place.

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

4 Comments

So it's not just Java, any programming language that supports concurrency should have a memory model?
Yes. The memory model is just how the language goes about using the hardware memory. If it wants to be multi-threaded it needs to take into account how memory access will behave if more than one core is accessing memory at a time.
Thank you for your answer, because when using python to develop multithreaded programs, I did not encounter the concept of memory model, only Java has, I hate to understand
Yes ... and the absence of a formal memory model in some languages is the root cause of many programming and portability problems!
1

Every programing language that takes concurrency seriously needs a memory model - and here is why.

The memory model is the crux of the concurrency semantics of shared-memory systems. It defines the possible values that a read operation is allowed to return for any given set of write operations performed by a concurrent program, thereby defining the basic semantics of shared variables. In other words, the memory model specifies the set of allowed outputs of a program's read and write operations, and constrains an implementation to produce only (but at least one) such allowed executions. The memory model may and often does allow executions where the outcome cannot be inferred from the order in which read and write operations occur in the program. It is impossible to meaningfully reason about a program or any part of the programming language implementation without an unambiguous memory model. The memory model defines the possible outcomes of a concurrent programs read and write operations. Conversely, the memory model also defines which instruction reorderings may be permitted, either by the processor, the memory system, or the compiler.

This is an excerpt from the paper Memory Models for C/C++ Programmers which I have co-authored. Even though a large part of it is dedicated to the C++ memory model, it also covers more general areas - starting with the reason why we need a memory model in the first place, explaining the (intuitive) sequential consistent model, and finally the weaker memory models provided by current hardware in x86 and ARM/POWER CPUs.

Comments

1

The Java Memory Model answers the following question: What shall happen when multiple threads modify the same memory location.

And the answer the memory model gives is: If a program has no data races, then all executions of the program will appear to be sequentially consistent.

There is a great paper by Sarita V. Adve, Hans-J. Boehm about why the Java and C++ Memory Model are designed the way they are: Memory Models: A Case For Rethinking Parallel Languages and Hardware

From the paper:

We have been repeatedly surprised at how difficult it is to formalize the seemingly simple and fundamental property of “what value a read should return in a multithreaded program.”

Memory models, which describe the semantics of shared variables, are crucial to both correct multithreaded applications and the entire underlying implementation stack. It is difficult to teach multithreaded programming without clarity on memory models.

After much prior confusion, major programming languages are converging on a model that guarantees simple interleaving-based semantics for "data-race-free" programs and most hardware vendors have committed to support this model.

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.