In Java, Locking, Synchronization, and volatility all relate to the state of the Java memory model at a specific point in the code.
Normally, in Java, you cannot really tell what the memory model looks like for each thread. Volatile, synchronization, and other locks change the memory model so that you can make guarantees about it's state at specific code points.
There are books written about this... Java Concurrency in Practice is the most recommended (and I recommend it too).
What is generally accepted is that for any code, you should choose only one of these three tools to manage your code. You should not mix volatile statements with synchronization, and with java.util.concurrent.lock.* structures.
##Volatile
Volatile
volatile
is essentially only useful for ensuring that a change in one thread is propagated quickly to another thread. It is not particularly useful for operations that require multiple 'steps' because you still have race conditions between the steps.
In fact, compound operations like x++
are not safe even. Volatile ensures that, at the immediate point the volatile is used, that it is the same in all threads. It does not mean it is the same at the end of the operation in each thread.
In my experience, non-boolean volatile
variables are inevitably broken in some way, and boolean volatiles are misused at least half the time.
Admittedly my understanding of the full implications of volatile is incomplete, and is harder to quantify because I first learned Java at a time when volatile often simply did not work. volatile
has maybe changed a bit, but, in general, don't use it unless you know what you are doing.
##Double-checked locking
Double-checked locking
if (!processKilled) cancelProcess();
and then cancelProcess contains:
synchronized (processLock) { if (process != null && !processKilled) { boolean killed = process.kill(); if (killed) processKilled = true; } }
What you have here is double-checked locking. Read this paper by Bill Pugh on it.
......
VoiceOfUnreason has also answerd here. My answer is incomplete, but what I would have said is covered well over there
In Java, Locking, Synchronization, and volatility all relate to the state of the Java memory model at a specific point in the code.
Normally, in Java, you cannot really tell what the memory model looks like for each thread. Volatile, synchronization, and other locks change the memory model so that you can make guarantees about it's state at specific code points.
There are books written about this... Java Concurrency in Practice is the most recommended (and I recommend it too).
What is generally accepted is that for any code, you should choose only one of these three tools to manage your code. You should not mix volatile statements with synchronization, and with java.util.concurrent.lock.* structures.
##Volatile
volatile
is essentially only useful for ensuring that a change in one thread is propagated quickly to another thread. It is not particularly useful for operations that require multiple 'steps' because you still have race conditions between the steps.
In fact, compound operations like x++
are not safe even. Volatile ensures that, at the immediate point the volatile is used, that it is the same in all threads. It does not mean it is the same at the end of the operation in each thread.
In my experience, non-boolean volatile
variables are inevitably broken in some way, and boolean volatiles are misused at least half the time.
Admittedly my understanding of the full implications of volatile is incomplete, and is harder to quantify because I first learned Java at a time when volatile often simply did not work. volatile
has maybe changed a bit, but, in general, don't use it unless you know what you are doing.
##Double-checked locking
if (!processKilled) cancelProcess();
and then cancelProcess contains:
synchronized (processLock) { if (process != null && !processKilled) { boolean killed = process.kill(); if (killed) processKilled = true; } }
What you have here is double-checked locking. Read this paper by Bill Pugh on it.
......
VoiceOfUnreason has also answerd here. My answer is incomplete, but what I would have said is covered well over there
In Java, Locking, Synchronization, and volatility all relate to the state of the Java memory model at a specific point in the code.
Normally, in Java, you cannot really tell what the memory model looks like for each thread. Volatile, synchronization, and other locks change the memory model so that you can make guarantees about it's state at specific code points.
There are books written about this... Java Concurrency in Practice is the most recommended (and I recommend it too).
What is generally accepted is that for any code, you should choose only one of these three tools to manage your code. You should not mix volatile statements with synchronization, and with java.util.concurrent.lock.* structures.
Volatile
volatile
is essentially only useful for ensuring that a change in one thread is propagated quickly to another thread. It is not particularly useful for operations that require multiple 'steps' because you still have race conditions between the steps.
In fact, compound operations like x++
are not safe even. Volatile ensures that, at the immediate point the volatile is used, that it is the same in all threads. It does not mean it is the same at the end of the operation in each thread.
In my experience, non-boolean volatile
variables are inevitably broken in some way, and boolean volatiles are misused at least half the time.
Admittedly my understanding of the full implications of volatile is incomplete, and is harder to quantify because I first learned Java at a time when volatile often simply did not work. volatile
has maybe changed a bit, but, in general, don't use it unless you know what you are doing.
Double-checked locking
if (!processKilled) cancelProcess();
and then cancelProcess contains:
synchronized (processLock) { if (process != null && !processKilled) { boolean killed = process.kill(); if (killed) processKilled = true; } }
What you have here is double-checked locking. Read this paper by Bill Pugh on it.
......
VoiceOfUnreason has also answerd here. My answer is incomplete, but what I would have said is covered well over there
In Java, Locking, Synchronization, and volatility all relate to the state of the Java memory model at a specific point in the code.
Normally, in Java, you cannot really tell what the memory model looks like for each thread. Volatile, synchronization, and other locks change the memory model so that you can make guarantees about it's state at specific code points.
There are books written about this... Java Concurrency in Practice is the most recommended (and I recommend it too).
What is generally accepted is that for any code, you should choose only one of these three tools to manage your code. You should not mix volatile statements with synchronization, and with java.util.concurrent.lock.* structures.
##Volatile
volatile
is essentially only useful for ensuring that a change in one thread is propagated quickly to another thread. It is not particularly useful for operations that require multiple 'steps' because you still have race conditions between the steps.
In fact, compound operations like x++
are not safe even are not safe even. Volatile ensures that, at the immediate point the volatile is used, that it is the same in all threads. It does not mean it is the same at the end of the operation in each thread.
In my experience, non-boolean volatile
variables are inevitably broken in some way, and boolean volatiles are misused at least half the time.
Admittedly my understanding of the full implications of volatile is incomplete, and is harder to quantify because I first learned Java at a time when volatile often simply did not work. volatile
has maybe changed a bit, but, in general, don't use it unless you know what you are doing.
##Double-checked locking
if (!processKilled) cancelProcess();
and then cancelProcess contains:
synchronized (processLock) { if (process != null && !processKilled) { boolean killed = process.kill(); if (killed) processKilled = true; } }
What you have here is double-checked locking. Read this paper by Bill Pugh on it.
......
VoiceOfUnreason has also answerd here. My answer is incomplete, but what I would have said is covered well over there
In Java, Locking, Synchronization, and volatility all relate to the state of the Java memory model at a specific point in the code.
Normally, in Java, you cannot really tell what the memory model looks like for each thread. Volatile, synchronization, and other locks change the memory model so that you can make guarantees about it's state at specific code points.
There are books written about this... Java Concurrency in Practice is the most recommended (and I recommend it too).
What is generally accepted is that for any code, you should choose only one of these three tools to manage your code. You should not mix volatile statements with synchronization, and with java.util.concurrent.lock.* structures.
##Volatile
volatile
is essentially only useful for ensuring that a change in one thread is propagated quickly to another thread. It is not particularly useful for operations that require multiple 'steps' because you still have race conditions between the steps.
In fact, compound operations like x++
are not safe even. Volatile ensures that, at the immediate point the volatile is used, that it is the same in all threads. It does not mean it is the same at the end of the operation in each thread.
In my experience, non-boolean volatile
variables are inevitably broken in some way, and boolean volatiles are misused at least half the time.
Admittedly my understanding of the full implications of volatile is incomplete, and is harder to quantify because I first learned Java at a time when volatile often simply did not work. volatile
has maybe changed a bit, but, in general, don't use it unless you know what you are doing.
##Double-checked locking
if (!processKilled) cancelProcess();
and then cancelProcess contains:
synchronized (processLock) { if (process != null && !processKilled) { boolean killed = process.kill(); if (killed) processKilled = true; } }
What you have here is double-checked locking. Read this paper by Bill Pugh on it.
......
VoiceOfUnreason has also answerd here. My answer is incomplete, but what I would have said is covered well over there
In Java, Locking, Synchronization, and volatility all relate to the state of the Java memory model at a specific point in the code.
Normally, in Java, you cannot really tell what the memory model looks like for each thread. Volatile, synchronization, and other locks change the memory model so that you can make guarantees about it's state at specific code points.
There are books written about this... Java Concurrency in Practice is the most recommended (and I recommend it too).
What is generally accepted is that for any code, you should choose only one of these three tools to manage your code. You should not mix volatile statements with synchronization, and with java.util.concurrent.lock.* structures.
##Volatile
volatile
is essentially only useful for ensuring that a change in one thread is propagated quickly to another thread. It is not particularly useful for operations that require multiple 'steps' because you still have race conditions between the steps.
In fact, compound operations like x++
are not safe even. Volatile ensures that, at the immediate point the volatile is used, that it is the same in all threads. It does not mean it is the same at the end of the operation in each thread.
In my experience, non-boolean volatile
variables are inevitably broken in some way, and boolean volatiles are misused at least half the time.
Admittedly my understanding of the full implications of volatile is incomplete, and is harder to quantify because I first learned Java at a time when volatile often simply did not work. volatile
has maybe changed a bit, but, in general, don't use it unless you know what you are doing.
##Double-checked locking
if (!processKilled) cancelProcess();
and then cancelProcess contains:
synchronized (processLock) { if (process != null && !processKilled) { boolean killed = process.kill(); if (killed) processKilled = true; } }
What you have here is double-checked locking. Read this paper by Bill Pugh on it.
......
VoiceOfUnreason has also answerd here. My answer is incomplete, but what I would have said is covered well over there
In Java, Locking, Synchronization, and volatility all relate to the state of the Java memory model at a specific point in the code.
Normally, in Java, you cannot really tell what the memory model looks like for each thread. Volatile, synchronization, and other locks change the memory model so that you can make guarantees about it's state at specific code points.
There are books written about this... Java Concurrency in Practice is the most recommended (and I recommend it too).
What is generally accepted is that for any code, you should choose only one of these three tools to manage your code. You should not mix volatile statements with synchronization, and with java.util.concurrent.lock.* structures.
##Volatile
volatile
is essentially only useful for ensuring that a change in one thread is propagated quickly to another thread. It is not particularly useful for operations that require multiple 'steps' because you still have race conditions between the steps.
In fact, compound operations like x++
are not safe even. Volatile ensures that, at the immediate point the volatile is used, that it is the same in all threads. It does not mean it is the same at the end of the operation in each thread.
In my experience, non-boolean volatile
variables are inevitably broken in some way, and boolean volatiles are misused at least half the time.
Admittedly my understanding of the full implications of volatile is incomplete, and is harder to quantify because I first learned Java at a time when volatile often simply did not work. volatile
has maybe changed a bit, but, in general, don't use it unless you know what you are doing.
##Double-checked locking
if (!processKilled) cancelProcess();
and then cancelProcess contains:
synchronized (processLock) { if (process != null && !processKilled) { boolean killed = process.kill(); if (killed) processKilled = true; } }
What you have here is double-checked locking. Read this paper by Bill Pugh on it.
......
VoiceOfUnreason has also answerd here VoiceOfUnreason has also answerd here. My answer is incomplete, but what I would have said is covered well over there
In Java, Locking, Synchronization, and volatility all relate to the state of the Java memory model at a specific point in the code.
Normally, in Java, you cannot really tell what the memory model looks like for each thread. Volatile, synchronization, and other locks change the memory model so that you can make guarantees about it's state at specific code points.
There are books written about this... Java Concurrency in Practice is the most recommended (and I recommend it too).
What is generally accepted is that for any code, you should choose only one of these three tools to manage your code. You should not mix volatile statements with synchronization, and with java.util.concurrent.lock.* structures.
##Volatile
volatile
is essentially only useful for ensuring that a change in one thread is propagated quickly to another thread. It is not particularly useful for operations that require multiple 'steps' because you still have race conditions between the steps.
In fact, compound operations like x++
are not safe even. Volatile ensures that, at the immediate point the volatile is used, that it is the same in all threads. It does not mean it is the same at the end of the operation in each thread.
In my experience, non-boolean volatile
variables are inevitably broken in some way, and boolean volatiles are misused at least half the time.
Admittedly my understanding of the full implications of volatile is incomplete, and is harder to quantify because I first learned Java at a time when volatile often simply did not work. volatile
has maybe changed a bit, but, in general, don't use it unless you know what you are doing.
##Double-checked locking
if (!processKilled) cancelProcess();
and then cancelProcess contains:
synchronized (processLock) { if (process != null && !processKilled) { boolean killed = process.kill(); if (killed) processKilled = true; } }
What you have here is double-checked locking. Read this paper by Bill Pugh on it.
......
VoiceOfUnreason has also answerd here. My answer is incomplete, but what I would have said is covered well over there
In Java, Locking, Synchronization, and volatility all relate to the state of the Java memory model at a specific point in the code.
Normally, in Java, you cannot really tell what the memory model looks like for each thread. Volatile, synchronization, and other locks change the memory model so that you can make guarantees about it's state at specific code points.
There are books written about this... Java Concurrency in Practice is the most recommended (and I recommend it too).
What is generally accepted is that for any code, you should choose only one of these three tools to manage your code. You should not mix volatile statements with synchronization, and with java.util.concurrent.lock.* structures.
##Volatile
volatile
is essentially only useful for ensuring that a change in one thread is propagated quickly to another thread. It is not particularly useful for operations that require multiple 'steps' because you still have race conditions between the steps.
In fact, compound operations like x++
are not safe even. Volatile ensures that, at the immediate point the volatile is used, that it is the same in all threads. It does not mean it is the same at the end of the operation in each thread.
In my experience, non-boolean volatile
variables are inevitably broken in some way, and boolean volatiles are misused at least half the time.
Admittedly my understanding of the full implications of volatile is incomplete, and is harder to quantify because I first learned Java at a time when volatile often simply did not work. volatile
has maybe changed a bit, but, in general, don't use it unless you know what you are doing.
##Double-checked locking
if (!processKilled) cancelProcess();
and then cancelProcess contains:
synchronized (processLock) { if (process != null && !processKilled) { boolean killed = process.kill(); if (killed) processKilled = true; } }
What you have here is double-checked locking. Read this paper by Bill Pugh on it.
......
VoiceOfUnreason has also answerd here. My answer is incomplete, but what I would have said is covered well over there