Posts

Showing posts with the label concurrency

Java Concurrency: Final

Image
What does it mean by FINAL? (Definition below only applicable to JDK6+) 1) Address of enclosing object not allowed to escape util final variable for initialized and change made to memory done 2) JVM can execute special CPU caching instruction 3) All change in constructor to final variable, even reference will be pushed to memory

Java Synchronization: volatile

Image
What does it mean by "volatile"? New JMM (1.5+) 1) Volatile variable reads from memory and write to memory instead of local cache Picture below shows example of a multi core system. Each CPU  has cache, which is super fast memory locally for that CPU. For optimization purpose, many information stored in cache. But in multi threaded application which shared variable used, if each CPU update and fetch info from its own cache, value of the shared variable is wrong. Example: a counter By using volatile, every time volatile variable read, it is fetched from memory. Every time volatile variable written, it is pushed from L1 to L2 to memory. 2) Instruction which use volatile variable cannot be reordered 3) Volatile variable observes what happened  For example, old value of w = 0 , x = 0 , f = true. f is a volatile variable. Now CPU1 updates x = 2 and f = false. As f is volatile, it is flushed to RAM. x is not volatile, however f observes that x changes as well, so...

Java Object Size and Overhead

Let's look at the details few details of object header and calculate the memory size an object occupies inside JVM Heap. Each Object contains following information. • The Object Header. • The memory for primitive types. 
 • The memory for reference types. 
 • Offset / alignment -  in fact, these are a few unused bytes that are placed after the data object itself. This is done in order that an address in memory was always a multiple of machine word, to speed up the memory read + reduce the number of bits for a pointer to an object. It is also worth noting that in java a size of any object is multiple of 8 bytes! •   Object Header :             In case of 32-bit system, the header size is 8 bytes, in the case of 64-bit system, respectively is 16 bytes. It contains following information. 1   Hash Code - 2   Garbage Collection Information  -  each java object contains the i...

Java Synchronization: The Reader

Image
Which methods need synchronized? Consider the following class stub. At this point there is no doubt we need to give "synchronized" to add() and remove(). How about get()? get() just does a read. Does it need a lock?  Method like get() needs synchronized too 1) Avoid inconsistent data get() should not look inside structure while some other threads are making multiple operator change because it could be inconsistent. 2) To get latest data from memory, not local cache The following image describing cache system of a multi-core processor.  As you can see each core has its own cache. The only shared memory between cores are RAM (memory).  Let say the original value if 1|2|3.  Processor 0 Core 0 running thread 1. Reading. Processor 1 Core 1 running thread 2. Writing. Thread 2 makes change to value to make it 1|2|3|4. Since add() is synchronized, the new value got added in P1C1 cache level 1, push to cache ...

Java Synchronization: The Lock

Where is the lock? Consider the following use case: I have class A which have a static variable i. I have method m1() which increase value of i by one everything it is called.  In a single thread system, it is simple. In multiple-thread system, value of i does not get set properly sometimes. Try to get the following code run by 2 threads a few times, we get inconsistent last value of i and some of them does not even get the last value to be 200.   public class A { static int i; public void m1() { for (int j = 0; j < 100; j++) { i++; System.out.println(i); } } } Why? Because i++ is not an atomic instruction. If we translate it to byte code, it will be something like: 1 - Read var = i 2 - Increase var by 1 3 - i = var Let say current value of i = 6. Thread 1 is trying to make it to 7. The first step done var = 7. Increase var by 1 so var = 8. Before thread 1 got chance to write back, thread 2 comes in and read i = 7. Thread 2 increa...

Java Synchronization: Keyword “synchronized”

Image
What does it mean by “synchronized”? 1) Mutual Exclusive Only one thread can enter synchronized block. 2) Guarantee no reordering Instruction inside synchronized block can reordered between themselves. E.g. 4, 6, 5 Instruction outside synchronized block can reorder between themselves. E.g. 2, 3 ,1. Instruction in synchronized block cannot be reordered to mix with outside block (1, 5, 2, 3) Instruction outside synchronized block cannot be reordered to mix with in block (7, 5, 8, 9) 3) Guarantee to get value from memory all the time, not from cache 4) Volatile variable observe next page (need validation) After all instruction in a synchronized block get executed. All change values are pushed into memory from local cache of the thread. Also as we know data move does not work on single variable, therefore the cache line which contains the changed value got pushed to memory too. Cost of the lock in "synchronized" 1) Memory Synchronization 2) Mutual Exclusi...

2018 Concurrency Training Note

2018 Concurrency Training Node Hotspot Compiler Brief Java Synchronization: Keyword Synchronize Java Synchronization: The Lock Java Synchronization: The Reader Volatile