java-memory-model

Threadsafe publishing of java object structure ?

Assuming that I have the following code: final Catalog catalog = createCatalog(); for (int i = 0; i< 100; i++{ new Thread(new CatalogWorker(catalog)).start(); } "Catalog" is an object structure, and the method createCatalog() and the "Catalog" object structure has not been written with concurrency in mind. There are several non-f...

Memory effects of synchronization in Java

JSR-133 FAQ says: But there is more to synchronization than mutual exclusion. Synchronization ensures that memory writes by a thread before or during a synchronized block are made visible in a predictable manner to other threads which synchronize on the same monitor. After we exit a synchronized block, we release the ...

Event Dispatch Thread meets the Java Memory Model

This is related to an earlier question I asked, where the answer was: If a field is accessed by multiple threads, it should be volatile or final, or accessed only with synchronized blocks. Otherwise, assigned values may not be visible to other threads. In addition anything that manipulates pixels on screen should be run f...

Java concurrency question - synchronizing on a collection

Will the following code snippet of a synchronized ArrayList work in a multi-threaded environment? class MyList { private final ArrayList<String> internalList = new ArrayList<String>(); void add(String newValue) { synchronized (internalList) { internalList.add(newValue); } } boolean find(Stri...

Java heap terminology: young, old and permanent generations?

I'm trying to understand how the concepts of young, old and permanent generations in the Java heap terminology, and more specifically the interactions between the three generations. My questions are: What is the young generation? What is the old generation? What is the permanent generation? How does the three generations interact/rela...

Multithreaded Java Program for Conway's game of life - contention at the border cells

I am learning concurrent programming in java, and writing a simulation for Game of Life. Here is what I am thinking: Use int[][] to store the states of the cells partition the int[][] into t segments and use t worker threads The t threads will read from their segment, calculate new values for all the cells in their segment and update ...

Java: volatile guarantees and out-of-order execution

IMPORTANT EDIT I know about the "happens before" in the thread where the two assignments are happening my question is would it be possible for another thread to be reading "b" non-null while "a" is still null. So I know that if you're calling doIt() from the same thread as the one where you previously called setBothNonNull(...) then it c...

Scala and the Java Memory Model

The Java Memory Model (since 1.5) treats final fields differently to non-final fields. In particular, provided the this reference doesn't escape during construction, writes to final fields in the constructor are guaranteed to be visible on other threads even if the object is made available to the other thread via a data race. (Writes to ...

Java LockSupport Memory Consistency

Java 6 API question. Does calling LockSupport.unpark(thread) have a happens-before relationship to the return from LockSupport.park in the just-unparked thread? I strongly suspect the answer is yes, but the Javadoc doesn't seem to mention it explicitly. ...

How can CopyOnWriteArrayList be thread-safe?

I've taken a look into OpenJDK's sources of CopyOnWriteArrayList and it seems that all write operations are protected by the same lock and read operations are not protected at all. As I understand, under JMM all accesses to a variable (both read and write) should be protected by lock or reordering effects may occur. For example, set(int...

Double-checked locking for growable array of binomial coefficients

I'm trying to use double-checked locking to maintain an array of binomial coefficients, but I read recently that double-checked locking doesn't work. Efficiency is extremely important so using volatile isn't an option unless it's only inside the conditional statements. I can't see a way to use a static class with a singleton object (th...

Behaviour of sychronized

I have read that code inside a synchronized block conform to 'happens before' semantics so that all the values written inside a synchronized block should be visible to other threads in succession. Furthermore I have read that caches are only flushed with the termination of synchronized block. How would above semantic of 'happens before' ...

java.util.concurrent: Should I synchronize to avoid visiblity issues among threads?

I wonder if when using any of the java.util.concurrent classes I still need to synchronize access on the instance so to avoid visibility issues. In short the question is: When using an instance of java.util.concurrent, is it possible that one thread modify the instance (i.e., put an element in a concurrent hashmap) and a subsequent th...