News from August, 2011

  2011/08/08
Unknown Corner of Java - Volatile Variables
Last changed: Aug 08, 2011 15:37 by Bartolomeo Sorrentino

Introduction

This is an extract from the interesting article 5 things you didn't know about ... multithreaded Java programming. It has focused on volative variables a truly unknown corner of java.

Volatile variables

I estimate that roughly half of all Java developers know that the Java language includes the keyword volatile. Of those, only about 10 percent know what it means, and even fewer know how to use it effectively. In short, identifying a variable with the volatile keyword means that the variable's value will be modified by different threads. To fully understand what the volatile keyword does, it's first helpful to understand how threads treat non-volatile variables.

In order to enhance performance, the Java language specification permits the JRE to maintain a local copy of a variable in each thread that references it. You could consider these "thread-local" copies of variables to be similar to a cache, helping the thread avoid checking main memory each time it needs to access the variable's value.

But consider what happens in the following scenario: two threads start and the first reads variable A as 5 and the second reads variable A as 10. If variable A has changed from 5 to 10, then the first thread will not be aware of the change, so it will have the wrong value for A. If variable A were marked as being volatile, however, then any time a thread read the value of A, it would refer back to the master copy of A and read its current value.

If the variables in your applications are not going to change, then a thread-local cache makes sense. Otherwise, it's very helpful to know what the volatile keyword can do for you.


Volatile versus synchronized

If a variable is declared as volatile, it means that it is expected to be modified by multiple threads. Naturally, you would expect the JRE to impose some form of synchronization for volatile variables. As luck would have it, the JRE does implicitly provide synchronization when accessing volatile variables, but with one very big caveat: reading a volatile variable is synchronized and writing to a volatile variable is synchronized, but non-atomic operations are not.

What this means is that the following code is not thread safe:

myVolatileVar++;

The previous statement could also be written as follows:

 int temp = 0;
 synchronize( myVolatileVar ) {
 temp = myVolatileVar;
 } 

temp++;

synchronize( myVolatileVar ) {
myVolatileVar = temp;
} 

In other words, if a volatile variable is updated such that, under the hood, the value is read, modified, and then assigned a new value, the result will be a non-thread-safe operation performed between two synchronous operations. You can then decide whether to use synchronization or rely on the JRE's support for automatically synchronizing volatile variables. The better approach depends on your use case: If the assigned value of the volatile variable depends on its current value (such as during an increment operation), then you must use synchronization if you want that operation to be thread safe.


Posted at 08 Aug @ 3:26 PM by Bartolomeo Sorrentino | 0 Comments
  2011/08/21
BeanManager 1.4.1 is out
Last changed: Aug 21, 2011 12:46 by Bartolomeo Sorrentino
Information

BeanManager is a lightweight framework that helps to deal with Object Relational Mapping problem.
The major feature of release 1.4 is the possibility to use Java Persistent Api (JPA) approach to define mapping.

project home

Availability

From this release the asset is available from PUBLIC MAVEN REPO provided by SONATYPE

Relocation

The groupId has been changed from org.bsc.framework to org.bsc.bean so to include dependency from maven you have to use the following definition

    <dependency>
      <groupId>org.bsc.bean</groupId>
      <artifactId>beanmanager-core</artifactId>
      <version>1.4.1</version>
    </dependency>

    <dependency>
      <groupId>org.bsc.bean</groupId>
      <artifactId>beanmanager-jpa</artifactId>
      <version>1.4.1
    </dependency>
Posted at 21 Aug @ 12:12 PM by Bartolomeo Sorrentino | 0 Comments
  2011/08/31
Eclipse Tip - Creating and Sharing Launch Configurations
Last changed: Aug 31, 2011 17:17 by Bartolomeo Sorrentino

Introduction

Typically when we share eclipse projects , in particular if they use maven, it should be very useful and productive to share the "Launch Configuration".

I've found this interesting article from Eclipse Snippets that describes, step by step, how to achieve this >> continue

Posted at 31 Aug @ 5:15 PM by Bartolomeo Sorrentino | 0 Comments