In the Java programming language, the final keyword is used in several different contexts to define an entity which cannot later be changed (or reassigned).
Showing posts with label java keywords. Show all posts
Showing posts with label java keywords. Show all posts
Wednesday, November 21, 2012
Tuesday, April 5, 2011
Java volatile
volatile keyword is used to indicate that a variable's value will be modified by different threads.
Declaring a volatile Java variable means:
We say "acts as though" in the second point, because to the programmer at least (and probably in most JVM implementations) there is no actual lock object involved. Here is how synchronized and volatile compare:
Difference between synchronized and volatile
An example:
Declaring a volatile Java variable means:
- The value of this variable will never be cached thread-locally: all reads and writes will go straight to "main memory";
- Access to the variable acts as though it is enclosed in a synchronized block, synchronized on itself.
We say "acts as though" in the second point, because to the programmer at least (and probably in most JVM implementations) there is no actual lock object involved. Here is how synchronized and volatile compare:
Difference between synchronized and volatile
| Characteristic | Synchronized | Volatile |
|---|---|---|
| Type of variable | Object | Object or primitive |
| Null allowed? | No | Yes |
| Can block? | Yes | No |
| All cached variables synchronized on access? | Yes | From Java 5 onwards |
| When synchronization happens | When you explicitly enter/exit a synchronized block | Whenever a volatile variable is accessed. |
| Can be used to combined several operations into an atomic operation | Yes | Pre-Java 5, no. Atomic get-set of volatiles possible in Java 5. |
An example:
public class StoppableTask extends Thread {
private volatile boolean stopThread;
public void run() {
while( !stopThread ) {
// do something
}
}
public void stopMe() {
stopThread = true;
}
}
Monday, March 28, 2011
Keywords in Java
The keywords cannot be used as identifiers.
assert - keyword from java 1.4
abstract
boolean
break
byte
case
catch
char
class
const
continue
default
do
double
else
enum - keyword from java 1.5
extends
final
finally
float
for
goto
if
implements
import
instanceof
int
interface
long
native
new
package
private
protected
public
return
short
static
strictfp
super
switch
synchronized
this
throw
throws
transient
try
void
volatile
while
assert - keyword from java 1.4
abstract
boolean
break
byte
case
catch
char
class
const
continue
default
do
double
else
enum - keyword from java 1.5
extends
final
finally
float
for
goto
if
implements
import
instanceof
int
interface
long
native
new
package
private
protected
public
return
short
static
strictfp
super
switch
synchronized
this
throw
throws
transient
try
void
volatile
while
Subscribe to:
Posts (Atom)