Skip to main content

Posts

Showing posts with the label Volatile

What is the diffrence between "volatile" and "synchronized" keywords?

Volatile keyword is thought of as an easy form of synchronized. It require less coding than synchronized and frequently have less run-time overhead. When we talk about locks, 2 main features introduce: i.) mutual exclusion which means only one thread at a time can hold a given lock, and ii.) visibility which ensures that changes mode to shared data before releasing the lock are accessible to another thread that subsequently acquires the lock. Comparing the volatile keyword with synchronized, the volatile is a field modifier while synchronized is used with code blocks and methods. Let's clear one more point here. Threads can have local copies of variables/data which don't have to be the same as the data held in other threads. In fact, java has an idea of a MAIN memory which holds the current value for variables. And threads can have their own copy of variables which can be different from the main memory. And it's possible that suppose you have a variable named X which value...

When should I use volatile modifier?

-- A variable that might be concurrently modified by multiple threads (without locks or a similar form of mutual exclusion) should be declared volatile. The volatile modifier can be used to inform the compiler that it should not attempt to perform optimizations on the field, which could cause unpredictable results when the field is accessed by multiple threads. For example, A multiprocessor system(>2 CPUs) with 2 threads sharing a single variable in memory. Thread 1 reads the variable and caches it in one of the registers for efficient access. Thread 2 reads the memory and changes its value. Now if Thread 1 wants to access the value, it may get it from its cache and thus end up with a wrong value. To avoid these scenarios a variable can be declared as volatile , thus informing the JVM that the value can change asynchronously and has to be fetched always from memory or in other words it is like saying to JVM "Please do not optimize this variable for any...

What is Transient, Volatile and Strictfp

Transient: The transient modifier applies to variables only and it is not stored as part of its object's Persistent state. These variables are not serialized. Transient instance fields are neither saved nor restored by the standard serialization . You have to handle restoring them yourself. Volatile: Volatile modifier tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of the program. For example a Variable might be read from Cache and not update the content if it has been changed by another thread. Specifying a variable as volatile tells the JVM that any threads using that variable are not allowed to cache that value at all. Making the Variable Volatile will ensure that the compiler will get the content of the variable every time it is used and not cache its content. If not used Carefully this modifier might introduce bugs.. Strictfp : Used to restrict floating point calculations ( fp ) to ensure portability ( platform In...