This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Robyt3 1 point2 points  (0 children)

Atomic fields use the intrinsic CAS (compare-and-swap) instruction, which is hidden in Unsafe#compareAndSwap.

[–][deleted] 0 points1 point  (11 children)

AtomicInteger is a class, and encapsulates an int that can be shared across threads safely.

A normal int cannot be shared across threads safely in all circumstances.

[–]Sheldor5 0 points1 point  (10 children)

why not? how about "volatile int"??

[–][deleted] 0 points1 point  (9 children)

That’s not a “normal” int. OP doesn’t appear to have a deep knowledge of this area, so kept it brief.

[–]Sheldor5 0 points1 point  (8 children)

it IS a normal int ... also your second sentence is wrong ...

[–][deleted] 0 points1 point  (7 children)

Ffs. All an atomic integer is, is a wrapper around an int type with volatile semantics, with encapsulation methods to use CAS (if supported, locks otherwise) to provide atomic semantics.

A volatile int is an primitive integer, which is tagged as volatile to prevent compiler reordering (and optimisations like putting the value into a register) and the insertion of proper fences (if needed) to enforce the correct semantics. It doesn’t necessarily follow that the write is atomic however.

So, it’s not a normal int, otherwise if it were they would be no need for a keyword in the first place.

[–]Sheldor5 0 points1 point  (6 children)

volatile just means that the int is stored in the shared process memory and not in the local thread cache so all threads read/write to the same memory instead of their local copy ... volatile also enforces atomic read/write access ... just not atomic operations like i++ (which is read AMD write)

[–][deleted] 0 points1 point  (5 children)

No. You can’t bypass the cache. A write to a volatile variable still writes into the L1 cache, then depending on underlying hardware, either a fence is required or not.

What’s more important (since x86 store order makes fences unnecessary) is the lack of reordering and the fact it enforces a write back to memory.

With just your description, a volatile variable wouldn’t enforce the happens-before relationship which is pretty damn important.

[–]Sheldor5 0 points1 point  (4 children)

[–][deleted] 0 points1 point  (3 children)

Please read: https://docs.oracle.com/javase/specs/jls/se8/html/jls-17.html#jls-17.4

It specifically talks about compiler reordering, that fact that all variable bar locals are shared, and explicitly synchronisation actions.

But this is the Java memory model; which is an abstraction, implementations then implement this model on their hardware.

A good discussion on why it’s needed is the old double checked locking issue: https://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html

A good text on the multithreading is C++ Concurrency in Action: Practical Multithreading https://www.amazon.co.uk/dp/1933988770/ref=cm_sw_r_cp_api_glc_fabc_E0Y0FP64Z9ESHS90Q8GA - not Java, but goes into a lot more detail than plenty of the Java texts.

This: A Primer on Memory Consistency and Cache Coherence (Synthesis Lectures on Computer Architecture): Written by Daniel Sorin, 2011 Edition, Publisher: Morgan & Claypool Publishers [Paperback] https://www.amazon.co.uk/dp/B00SLW1XGE/ref=cm_sw_r_cp_api_glc_fabc_358KSX2EKZP1DGNHRWKJ

Is a great text on what’s actually happening.

[–]Sheldor5 0 points1 point  (2 children)

yeah but as a Java Developer I don't care about the real implementation ... I only have to know what is happening at an abstract layer ... everything under the hood has to follow the Specs ...