Advanced Topics in Programming Languages: The Java Memory Model

GoogleTalksArchive · 127K views · 2012 · 57:23

Open on YouTube

Programming language designSoftware performance & optimizationC, C++, Rust, Go & systems languagestalk

Why Stack Overflow recommends it

Stack Overflow users link it when explaining visibility, happens-before and volatile or synchronized semantics in Java concurrency questions, often as the authoritative explanation from someone who helped write the model.

Recommended by 5 people across 4 different months, 2009 to 2016.

What it is

A Google Tech Talk from the Advanced Topics in Programming Languages series in which Jeremy Manson, a co-author of the revised Java Memory Model, explains what synchronization, volatile variables, java.util.concurrent.atomic and final fields actually guarantee. It covers the JDK 5 memory model changes and how to write correct concurrent code on top of them.

What people said

Recommends jb. · 2009-05 · Stack Overflow · on “What is the scope of cache flushing required by Java's synchronize keyword?”

There is very nice tech talk on Java Memory model [this video] . If you dislike wideos google 'happens before' in context of Java Memory Model. Basically all writes are visible to other threads if there is happens before relationship, Lets assume thaat thread A writes to field X, and thread B reads from it, so happens before is establishend between write and read if: x is volatile write to x was in guarded by the same lock that read from x maybe some more. So I think that the second option is true, how they implemented it, I dont know.

Recommends tgkprog · 2013-03 · Stack Overflow · on “Best way of running two threads alternatively?”

exchanger looks right. After watching [this video] I think the variable should be volatile, says there is hardly any over head.

Recommends Andrew Bissell · 2013-03 · Stack Overflow · on “Interpretation of "program order rule" in Java concurrency”

I highly recommend picking up a copy of Java Concurrency in Practice by Goetz et al. Although it's starting to show its age a little it is still something of a gold standard for concurrent programming references. You may also find this GoogleTalk by Jeremy Manson (who co-authored the memory model itself) to be helpful: [this video]

Recommends tgkprog · 2013-03 · Stack Overflow · on “Best way of running two threads alternatively?”

@clement-mathieu did you watch the youtube video [this video] ? Djebel what did you finally use? did you get to test on a multi core server with different implementations to see if it fails ever?

Recommends haylem · 2013-06 · Stack Overflow · on “Understanding the Java Memory Model and Garbage Collection”

Memory Basics What Every Programmer Should Know About Memory - Ulrich Drepper [ PDF [LINK] ] Java Language and Virtual Machine Specifications [LINK] The Java Virtual Machine Specification [ PDF [LINK] and HTML [LINK] for Java SE 7] Java Memory Model [LINK] Advanced Topics in Programming: The Java Memory Model - Jeremy Manson [ video [this video] ] Collection of Resources on the Java Memory Model - edited and compiled by Bill Pugh ( collection [LINK] ) Memory Management in the HotSpot Virtual Machine - Sun Microsystems (2006) [ PDF [LINK] ] Fixing the Java Memory Model - Brian Goetz, IBM Java Developer Series () [ part 1 [LINK] , part 2 [LINK] ] Java Performance 9 Fallacies of Java Performance - B

Recommends Timofey · 2016-02 · Stack Overflow · on “Good example of livelock?”

The `getOwner` method must be synchronized since even if the `setOwner` is synchronized, this does not guarantee the thread using `getOwner` (or accessing the field `owner` directly) will see the changes made by the other thread performing `setOwner`. This vid explains this very carefully: [this video]

Cites jb. · 2009-05 · Stack Overflow · on “Java synchronization and performance in an aspect”

Rule of thumb is not to synchronize on this - most of the times it is a performance hit - all methods are synchronized on one object. Consider using locks - they'a very nice abstraction and many fine features like, trying to lock for a time period, and then giving up: if(commandsLock.tryLock(100, TimeUnit.MILLISECONDS)){ try { //Do something }finally{ commandsLock.unlock(); } }else{ //couldnt acquire lock for 100 ms } I second opinion on using java.util.concurrent . I'd make two levls of synchronization synchronize collection access (if it is needed) synchronize field access Collection access If your collection are read-only ie no elements get removed-inserted (but elements may change) i wou

Cites tgkprog · 2013-05 · Stack Overflow · on “How to create a cyclic exchange of three threads?”

For your dependency you should make three classes, and have three distinct Exchange objects (one in each). So thread1 would be between 1 and 2 (output of 1 to 2), thread 2's would be between 2 and 3 and thread 3's exhanger would be between itself and 1. Remember the exchanger's would guard only until it had its input from its feeder, to till it passes to its receiver. Also synchronized is not as bad as the books make out. use it. Watch [this video] Also for reference Best way of running two threads alternatively? [LINK] Also why do you need three threads? Can you use a thread pool and have each task to the 3 things ?

Cites user2365209 · 2013-07 · Stack Overflow · on “Synchronized method ans synchronized blocks in Java”

I'm just starting with synchronization in Java and I have small question. Is this method: public synchronized void method() { // ... do staff ... } Is equal to: public void method() { synchronize(this) { // ... do staff ... } } PS Recently I watched 2 good confs about Java (and from this comes my question) video 1 [LINK] , video 2 [this video] . Do you have some relative videos (I'm interested in programming in Java and Android).