race conditions occurs when the correctness of a computation depends on the relative timing or interleaving of multiple threads by the scheduler

data race two or more threads(in single application) accesses the same memory location concurrently, at least one of the accesses is for writing, and these threads don't coordinate their accesses to that memory.

cached variables to boost performance, the compiler, the Java viritual machine(JVM) and the operating system can collaborate to cache a variable in register or a processor-local cache, rather than rely on main memory. Each thread has its own copy of the variable. when one thread writes to this variable, it's writing to its copy, other threads are unlikely to see the update in their copies.

Synchronization to solve the race condition, data race and cached variables thread problems. Synchronization is a JVM feature that ensures that two or more concurrent threads don't simultaneously execute a critical section. which is a code section that must be accessed in a serial(one thread at a time) manner.

this property of synchronization is known as mutual exclusion

synchronization is implemented in terms of monitors, which are concurrency constructs for controlling access to critical sections.

lock

synchronized method

synchronized keyword to serialized thread access to a method or a block of statements(the critical section)

synchronizing on an instance method, the lock is associated with the object on which the method is called

synchronizing on an class method

synchronized blocks

synchronized(object)

{

/* statements */

}

each block is guarded by the same object so that only one thread can execute on one of these blocks at a time. each

results matching ""

    No results matching ""