Skip to main content

Posts

Showing posts with the label Synchronization

Java Memory Model

Processors generally have one or more layers of memory cache (eg. L1 cache, L2 cache and L3 cache etc.) in multiprocessor systems which improves performance by speeding access to data (because the data is closer to the processor) and reducing traffic on the shared memory bus (because many memory operations can be satisfied by local caches.) Memory caches can improve performance tremendously, but they present a host of new challenges. For example - What would happens when two processors examine the same memory location at the same time? Under what conditions will they see the same value? A memory model defines necessary and sufficient conditions for knowing that writes to memory by other processors are visible to the current processor, and writes by the current processor are visible to other processors. Some processors exhibit a strong memory model, where all processors see exactly the same value for any given memory location at all times. Other processors exhibit a weaker memo...

Understanding Concurrency, Threading and Synchronization

Thread: A thread is defined at the operating system level. It is a single execution of a program. A thread is a set of instruction. An application can be composed of several threads. Different threads can be executed at the same time. The JVM works with several threads like Garbage collector, JIT etc. What does mean by "At the same time"? In a single core of CPU, the threads cannot works at the same time, it give us a feel that it is running at same but in reality it work one by one and the context switch happen very fast in milliseconds which we can't observe. On a multi-core CPU, things happens at the same time. A challenge while this is the read and write operation involve while running different thread at same time. Who is responsible for the CPU sharing? There is a special element who does this, is called scheduler.there are three reasons for a scheduled to pause a thread -  The CPU should be shared equally among threads. The thread might be waiting ...