Transient vs Volatile modifiers in Java
By Reema sen Viewed: 32248 times Emailed: 200 times Printed: 237 times
Java defines two interesting type modifiers: transient and volatile. These modifiers are used to handle somewhat specialized situations.
When an instance variable is declared as transient, then its value need not persist when an object is stored. For example:
class T {
transient int a; // will not persist
int b; // will persist
}
Here, if an object of type T is written to a persistent storage area, the contents of a would not be saved, but the contents of b would. The volatile modifier tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of your program. One of these situations involves multithreaded programs. In a multithreaded program, sometimes, two or more threads share the same instance variable. For efficiency considerations, each thread can keep its own, private copy of such a shared variable. The real (or master) copy of the variable is updated at various times, such as when a synchronized method is entered. While this approach works fine, it may be inefficient at times. In some cases, all that really matters is that the master copy of a variable always reflects its current state. To ensure this, simply specify the variable as volatile, which tells the compiler that it must always use the master copy of a volatile variable (or, at least, always keep any private copies up to date with the master copy, and vice versa). Also, accesses to the master variable must be executed in the precise order in which they are executed on any private copy.
in Java has, more or less, the same meaning that it has in C/C++.Note: volatile
Comments(15)
| 1. | Your explanation is good and okay but, it will be better or most lovely only if you can try to and some real java program code to illustrate what you are talking about better. |
| 2. | Your explanation is perfectly clear and not in need of "illustrative examples"! In particular, it succinctly makes clear that one should ALWAYS decorate an instance variable that is to be shared with "volatile". |
| 3. | Hi thanks for your information. |
| 4. | Hello, |
| 5. | If you are working with the multi-threaded programming, the volatile keyword will be more useful. When multiple threads using the same variable, each thread will have its own copy of the local cache for that variable. So, when it's updating the value, it is actually updated in the local cache not in the main variable memory. The other thread which is using the same variable doesn't know anything about the values changed by the another thread. To avoid this problem, if you declare a variable as volatile, then it will not be stored in the local cache. Whenever thread are updating the values, it is updated to the main memory. So, other threads can access the updated value. |
| 6. | You should consider using different threads that are not instantiated from the same class, but different classes. The volatile keyword does not work. The only workaround is to use another moitor class. I will try to sync and see if thqat will help. |
| 7. | If you are working with the multi-threaded programming, the volatile keyword will be more useful. When multiple threads using the same variable, each thread will have its own copy of the local cache for that variable. So, when it's updating the value, it is actually updated in the local cache not in the main variable memory. The other thread which is using the same variable doesn't know anything about the values changed by the another thread. To avoid this problem, if you declare a variable as volatile, then it will not be stored in the local cache. Whenever thread are updating the values, it is updated to the main memory. So, other threads can access the updated value. |
| 8. | Your explanation is good but, it will be better if you can try explain with some real java program code. |
| 9. | This is an excerpt from Java Complete Reference.Just copy and paste technology |
| 10. | Explanation is very good. Thanks all. |
| 11. | The example is wrong, because there are two seperate instances of ExampleThread, so the testValue is not shared by the threads |
| 12. | This example is working fine....I think BillGates has misunderstood somewhere.. |
| 13. | HI your explanation is absolutely correct but I am sure that it's copied from Complete reference Java2 fifth edition of Herbert Schildt. |
| 14. | Hi ,learn a new thing about These Transient and Volatile Modifiers . |
| 15. | Can some body throw more light on TRANSIENT .... |
Latest Tutorials
More Latest News
Most Viewed Articles (in last 30 days)

