Skip to main content

Volatile keyword in JAVA

I've found an interesting and easy to understand explanation about volatile keyword, it came with an example but it was wrong so I've corrected it.
A volatile variable does not have a copy maintained in the local memory of the thread (on the stack). All changes to the volatile variable (caused by multiple threads) are flushed out to the heap memory (visible from all threads). Hence volatile variable values remain consistent for all threads. 

On the other hand, for other instance variables, each java thread maintains a local copy on the stack. Multiple threads may modify this local copy of the instance variable and hence inconsistent values may be visible for multiple threads. 

For preventing this condition, we synchronize. During synchronization, a lock is first taken on the object monitor. Then the thread reads the state from the main memory and flushes its internal state. Subsequently, the synchronized code block/method code is executed. Once the execution completes, all the changes to the variables of that thread are flushed out to the main memory. Then the object monitor lock is released.

So, as we can see, volatile is a specialized case of synchronization. The only exceptions are, that it operates on a single field and no locks on the object monitor are required (as it operates on the heap memory and not the thread local stack memory).
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. 

class ExampleThread extends Thread {
  private volatile int testValue;

  public ExampleThread(String str) {
super(str);
  }

  public void run() {
for (int i = 0; i < 3; i++) {
 try {
System.out.println(Thread.currentThread().getName() + " : " + i);
if (Thread.currentThread().getName().equalsIgnoreCase("T1")) {
 System.out.println("Test Value T1: " + testValue);
 testValue = 10;
}
if (Thread.currentThread().getName().equalsIgnoreCase("T2")) {
 System.out.println("Test Value T2: " + testValue);
 testValue = 20;
}
Thread.sleep(1000);
 } catch (InterruptedException exception) {
exception.printStackTrace();
 }
}
  }
}

public class VolatileExample {
  public static void main(String args[]) {
ExampleThread exampleThread = new ExampleThread("Thread 1 ");
Thread t1 = new Thread(exampleThread, "T1");
Thread t2 = new Thread(exampleThread, "T2");
t1.start();
t2.start();

  }
}

Comments

Popular posts from this blog

Removing duplicates with awk

To remove duplicates in linux you can use awk command as follows:  awk -F- '!seen[$1]++' file.txt > output.txt where: -F is paraeter to deternime separator used to define columns and in ths case the dash 8-) is the sparator used '!seen[$1]++' is the expression used for the line in order to be printed > is the instruction to send command output o a file

wcs query to fetch shipping cost by store

In websphere commerce 7 model you can use the folloging query to fetch the shipping cost of catalog entries. This query could also be useful for newer versions. select cr.calrange_id,cc.calcode_id, c.partnumber,j.code,j.description,value from catentry c left outer join CATENCALCD cd on c.catentry_id=cd.catentry_id left outer join calcode cc on  cd.calcode_id=cc.calcode_id left outer join calrule r on cc.calcode_id=r.calcode_id left outer join CRULESCALE s on r.calrule_id=s.calrule_id left outer join CALRANGE cr on s.calscale_id=cr.calscale_id left outer join CALRLOOKUP cl on cr.CALRANGE_id=cl.CALRANGE_id left outer join SHPJCRULE sr on sr.calrule_id=r.calrule_id left outer join JURSTGROUP jg on jg.jurstgroup_id=sr.jurstgroup_id left outer join JURSTGPREL jgr on jgr.jurstgroup_id=jg.jurstgroup_id left outer join JURST j on j.jurst_id=jgr.jurst_id where (partnumber like 'partnumber% ) and catenttype_id in ('ItemBean','PackageBean') and cc.storeent_id=stor...

Diferencias del Metodo equals() y ==

Por ejemplo, el tipo de datos int, en Java, es un tipo de dato de los llamados primitivos (más o menos como c++), con lo cuál puedes utilizar los operadores lógicos y aritméticos de la forma habitual. En tu caso puedes utilizar el operador == de la manera normal. Sin embargo, Integer, no es un tipo de dato primitivo. Es una clase!!. Entoces cuando tú tienes un objeto del tipo Integer NO estás manejando su valor, sino un puntero a tu objeto Integer. Porque en Java todo son punteros (menos los tipos de datos primitivos). Así que cuando utilizas el operador == no estás comparando el valor de los integer, sino los punteros (las direcciones que apuntan al objeto). De modo que para objetos estás obligado a utilizar el método equals().