Brainteaser: Hidden Iterators

… While locking can prevent iterators from throwing ConcurrentMofdificationException, You have to remember to use locking everywhere a shared collection might be iterated. This is trickier than it sounds …

Brian Goetz p.83-84

Question: The following code could throw ConcurrentMofdificationException, even though add() is synchronized, why?

[java]
public class HiddenIterator {
private final Set set = new HashSet();

public synchronized void add(Integer i) {
set.add(i);
}

public synchronized void remove(Integer i) {
set.remove(i);
}

public void addTenThings() {
Random r = new Random();
for (int index = 0; index < 10; index++;) {
add(r.nextInt());
}

System.out.println("Added ten elements to set: " + set);
}
}
[/java]

Looking forward for your answers dear readers

Singleton Pattern and Problem With Double Checked Locking

There are several ways to initialize singleton object. Some are thread safe and some are not. Until the last two days I thought that double-checked locking idiom offers the best solution for singleton initialization in multi threaded environment. Well its not.

To begin, I want to describe briefly several ways to initialize singleton:

  • Lazy initialization
  • Eager initialization
  • DCL
  • Class holder lazy initialization
  • Enum singleton

Lazy initialization
The goal of lazy initialization was to defer initialization of an object until it is actually needed, while making sure that initialization is done only once. Lazy initialization example illustrated below:

[java]
public class Singleton{

private static Singleton instance = null;

private Singleton() { }

public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
[/java]

This will work fine in single-threaded environment. Unfortunately since getInstance() is not synchronized, two different instances of the object can be returned if two threads will access getInstance() method concurrently. Which makes this lazy initialization unsafe in multi threaded environment.

Unsafe lazy initialization can be fixed by making getInstance() synchronized:

[java]

public class Singleton{

private static Singleton instance = null;

private Singleton() { }

public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
[/java]

However, in this case there is performance hit for every invocation of getInstance() method. Synchronized methods runs very slow compared to unsynchronized methods. I haven’t tested this issue my self, so i cannot tell by how much slower.

Eager initialization
By using eager initialization, the synchronization cost incurred for each method call is eliminated:

[java]
public class Singleton{
private static Singleton instance = new Singleton();

private Singleton() { }

public static Singleton getInstance() {
return instance;
}
}
[/java]

The problem with eager initialization that Singleton object is instantiated regardless whether it is going to be used or not.

DCL
Double-checked locking (DCL) idiom was created to allow lazy initialization of a singleton object, without performance reducing costs as a result of synchronization. Unfortunately it does not work. Lets have a look why:

[java]
public class Singleton {

private static Singleton instance = null;

private Singleton() { }

public static Singleton getInstance() {
if (instance == null) {
synchronized(Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
[/java]

First there is a check whether initialization is needed with out synchronization, and if the instance is not null – then use it.
Otherwise, synchronize and check again if instance is null by making sure that only one thread will initialize the shared Singleton.

The problem with DCL is that code of fetching reference to constructed Singleton object does not require synchronization.
Lets say that thread ‘a’ has entered synchronized block, made the instance not null but hasn’t executed the constructor yet. At this stage thread ‘b’ preempts thread ‘a’. Thread ‘b’ checks if the instance is not null, and since its not, it returns reference to a partially constructed Singleton object.

In other words – thread ‘b’ sees a current value of the Singleton object reference instead of its stale value, which causes the object to be seen in invalid or incorrect state.

Class holder lazy initialization

Instead of DCL, lazy initialization class holder idiom offers better solution for singleton initialization in multi threaded environment, with the same benefits as DCL:

[java]
public class SingletonFactory {

private static class SingletonHolder {
public static Singleton instance = new Singleton();
}

public static Singleton getInstance() {
return SingletonHolder.instance;
}
}
[/java]

The SingletonHolder class initialization is deferred until its actually used, and because Singleton is initialized with static initializer, no additional synchronization is needed.
The first invocation of getInstance() by any thread causes SingletonHolder to be loaded and initialized during which time the Singleton is initialized through static initializer.

Enum singleton
Joshua Bloch in his book “Effective Java” offers a new interesting solution to initialize a Singleton by using Enum with a single type:

[java]
public class SingletonEnum {
INSTANCE;

public void someMethod() {

}

public void anotherMethod() {

}
}
[/java]

Joshua claims:

“…a single-element enum type is the best way to implement a singleton…”

Some people may argue against this approach by saying that this is not a class, but an enum – it enumarates plus enums cannot be subclassed. Well in case of a singleton there is no point in subclassing, since singletons have private constructors and singletons dont really ment to be subclassed. Lets have a look if Enum singleton is indeed the best solution so far:

1. Enum singleton is final
2. It is serializable
3. It is a single instance in multithreaded environment
4. Does not allow invokation of private constructors through reflection attacks

I think its quite original solution.

I will appreciate your comments regarding this post. Cheers.

Resources:
Effective Java
Java Concurrency in Practice

Double-checked locking and the Singleton pattern

Ouch!

Yesterday one of the developers that I work with lost all his work that he has done for the past two months! Apparently what happened was that the poor bagger did not commit anything to CVS for the past two months. So yesterday for some mysterious reason he has overridden his local changes with the outdated copy of the source code from CVS and tried to recompile everything. I don’t know why he did it.

I think he was almost crying telling me about the amount of work that was lost. During the conversation, it became obvious to me that for his luck, he had somewhere all the classes JARed as a backup (from before he has overridden the changes).

When I heard it, I said “Dude… you can decompile the classes! You can get the source code back” …

Ohhh… the look on his face after I said it – priceless …

Marshalling VS Serialization

The two terms are very very similar, and often treated the same. Quite few responds on various forums contradict each other, which made me a bit confused. So to make it clear for my self I decided to summarize what i understood from the resources I have read on the Internet:

Serialization:

  1. Technique of converting a state of an object (or object instance in different words) into a byte stream.
  2. To send object over the network, it needs to be serialized first, thus converted into to a sequence of bytes.

Marshalling:

  1. Technique of encoding an object to send it over the network and decoding it on the other end. Which is stub and skeleton mechanism, where stub is the local client and skeleton is the remote server.
  2. Besides the object state, marshalling also records codebases.
  3. Marshalling is used for sending objects through sockets. Off course objects must be serialized serialized before sending over the network.
  4. Marshalling is a way to create byte stream so that the copy of the original object can be recreated on the other side. When object is “unmarshalled” on the other side, object’s class definitions are loaded.
  5. Marshalling is a form of serialization, thats why I think some people treat teh two the same.

Thats it… I think… Did I miss something?

Java Generics and Reflection

Hi, the other day I had a situation, where in my code at run time I had to determine the super type of the Class that I obtained. To sub type my classes, I used generics.

In case my obtained Class was of expected super type, I had to invoke a super class static method on the subclass Class using reflection.

I used these concepts and prepared a short tutorial, I also attached source files to this post in case someone wants to download them. What I did was I created an abstract class, and an extending child class using generics. In my test client, I determine the super type of my child class at run time and then I invoke a static method using reflection.

Reflection also allows to invoke methods, constructors and change value of fields that are private. It is often called “reflection attack”. In my post Hack any Java class using reflection attack, I give several examples about reflection attack.

Ok, back to the topic now:

My abstract parent class:

[java]
import java.io.Serializable;
import java.util.Date;

public abstract class AbstractParentEntity
<E extends AbstractParentEntity>
implements Serializable {

private static final
long serialVersionUID = 5419258598746186610L;

public static Long getSomeLongValue() {
return new Long(999);
}

}
[/java]

My extending child class:

[java]
import java.io.Serializable;

public class ChildEntity extends
AbstractParentEntity<ChildEntity>
implements Serializable {

private static final long
serialVersionUID = -2271176823058287608L;

private String name;

public ChildEntity() {

}
}
[/java]

My test client:

[java]
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;

public class Test {

//canonical name of my child class
private static final String
CHILD_CLASS_NAME = "ChildEntity";
//name of the static method i am going to invoke
private static final String
PARENT_METHOD = "getSomeLongValue";

public static void main(String[] args) {

Thread thread = Thread.currentThread();

ClassLoader classLoader =
thread.getContextClassLoader();

try {

//use class loader to create a Class for
//a given class name
Class subclass = Class.forName(CHILD_CLASS_NAME,
true, classLoader);
System.out.println("Subclass canonical name: ["
+ subclass.getCanonicalName() + "]");

//get superclass Class from the subclass
Class superclass = subclass.getSuperclass();
System.out.println("Super class canonical name: ["
+ superclass.getCanonicalName() + "]");

//get the type of subclass
Type subtype = subclass.getGenericSuperclass();
System.out.println("Subclass type: ["
+ subtype.toString() + "]");

//check whether my subclass type starts with
//class name of my super type, if true – my subclass
//is indeed of type of my superclass
if (subtype.toString().startsWith(
superclass.getCanonicalName())) {

System.out.println("Class: [" +
subclass.getSimpleName()
+ "] is type of ["
+ superclass.getSimpleName() + "]");
}

// ‘null’ assumes empty array
Method method = subclass.getMethod(PARENT_METHOD,
(Class[]) null);

/*
* public Object invoke(Object obj, Object args)
*
* If the underlying method is static, then the specified ‘obj’
* argument is ignored. It may be null.
*
* Parameters: obj – the object the underlying method is invoked
* from args – the arguments used for the method call
*/

System.out.println("Invoking static parent method: [" +
PARENT_METHOD + "] on extending subclass…");
Object objResult = method.invoke(null, (Object[]) null);

System.out.println("Some value: [" + objResult.toString() + "]");

}
catch (SecurityException e) {
e.printStackTrace();
}
catch (NoSuchMethodException e) {
e.printStackTrace();
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}

}
}
[/java]

Basically what happens is: iI compare whether the Type of my subclass: AbstractParentEntity starts with the canonical name of my super class: AbstractParentEntity.

When using generics, this will always return true if my subclass extended from my super class.

When I successfully determined the subclass type, i invoked static method using reflection. Below you can see the output of my program:

[java]
Subclass canonical name: [ChildEntity]
Super class canonical name: [AbstractParentEntity]
Subclass type: [AbstractParentEntity<ChildEntity>]
Class: [ChildEntity] is type of [AbstractParentEntity]
Invoking static parent method: [getSomeLongValue] on extending subclass…
Some value: [999]
[/java]

If you never used generics in Java (those who moved on to Java from C++ will know what generics are), Sun offers a nice generics introduction tutorial which explains the basics.

Basically generics allow you to abstract over types. When you use generics in your code it becomes safer and clearer. So i think it worth while having a pick at it :)

Comments / corrections / flames?

Bitwise Operation In Hibernate 3

Hi all…
i encountered a small problem in doing bitwise operations with hibernate. Until now, HIbernate 2 HQL parser has supported bitwise operations. Hibernate 3 for some reason does not support it. So if you want to work around it, you have to create a custom SQLfunction and add it to the dialect, that will map the bitwise operator.

You have to create your own class, which will extend from StandardSQLFunction class and will implement SQLFunction interface. You have to override render() method from the interface mentioned before and provide your own implementation.

In my example I am showing how to use ampersand symbol (‘&’) for bitwise operation.

Important note:
Hibernate team people have changed SQLFunction interface when upgrading from version 3.0.2 to 3.0.3, so your custom implementation of bitwise operation in version 3.0.2 will not work in 3.0.3. So keep that in mind.

Update (13.Jan.2010):
According to one of the blog reader’s this example has successfully worked under Hibernate v.3.2.6!

Implementation:
I am using Sysbase dialect, so i will create a class that extends this dialect with my own implementation:

[java]
package com.project.test.dialect;

import org.hibernate.Hibernate;

public class SybaseDialect extends
org.hibernate.dialect.SybaseDialect{

public SybaseDialect() {
super();
registerFunction("bitwise_and",
new BitwiseAndFunction("bitwise_and",
Hibernate.INTEGER));
}

}
[/java]

What it means basically that i am registering a function by the name of bitwise_and the return type will be of type Integer.

Now, my custom class for SQLFunction like this:

[java]
package com.project.test.dialect;

import java.util.List;

import org.hibernate.QueryException;
import org.hibernate.dialect.function.SQLFunction;
import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.type.Type;

public class BitwiseAndFunction
extends StandardSQLFunction
implements SQLFunction {

public BitwiseAndFunction(String name) {
super(name);
}

public BitwiseAndFunction(String name, Type type) {
super(name, type);
}

public String render(List args,
SessionFactoryImplementor factory)
throws QueryException {
if (args.size() != 2) {
throw new IllegalArgumentException(
"the function must be passed 2 arguments");
}
StringBuffer buffer = new
StringBuffer(args.get(0).toString());
buffer.append(" & ").append(args.get(1));
return buffer.toString();
}
}
[/java]

The method render() accepts two arguments, if less – exception will be thrown. Once arguments are passed to the method, it will return a String that will look something like this (assuming that the passed arguments were one (1) and two (2)):

1 & 2

In my project, I am using named queries in my hbm.xml files, so below you can see a part of my hbm.xml file where I use my bitwise function:

[xml]
<query name="UserPermissionsForEnttiy">
select object(ep) from EntityPermission ep where ep.userid =
:userid and ep.entityid = :entityid and ep.classname =
:classname and bitwise_and(ep.permission,:requiredpermission) > 0
</query>
[/xml]

Note the two arguments that i am passing to the bitwise_and function.

Also not forget to update your persistence XML with your custom dialect class, this is how my persistence.xml looks like:

[xml]
<persistence>
<persistence-unit name="com.project.test.users">
<jta-data-source>java:/DefaultDS</jta-data-source>
<properties>
<property name="hibernate.dialect"
value="com.project.test.dialect.SybaseDialect"/>
<property name="hibernate.hbm2ddl.auto"
value="update"/>
<property name="hibernate.show_sql"
value="true"/>
.
.
.
</properties>
</persistence-unit>
</persistence>
[/xml]

Please note the hibernate.dialect property. As a value i gave the canonical name of my custom SysbaseDialect class.

Once you finished implementing your custom dialect classes, you can JAR them and put into the lib directory of your JBoss instance where the rest of your Hibernate libraries are.

Do not forget to restart your JBoss – it needs to load your newly added JAR, for you to be able to use it ;)

Multiple Return Statements

Yesterday I had a thought in my mind (which is good already to have one) – how many return statements a method should have?

Whats the difference if you have method that looks like that:

[java]
public int calculateSum(int a, int b, int c) {
int result = -1;
if (a % b == c) {
result = c;
}
else if ((a + b – c) > (a – c)) {
result = a;
}

return result;
}
[/java]

or the same method but looks like that:

[java]
public int calculateSum(int a, int b, int c) {
if (a % b == c) {
return c;
}
else if ((a + b – c) > (a – c)) {
return a;
}

return -1;
}
[/java]

I always thought (at least this is how i was taught), that having one return statement makes code look more elegant, and i should try to avoid having multiple return statements. When i asked my colleague what he thinks about this, he said that at run time, method with multiple returns will execute faster and the code looks clearer. That sounded logical enough, and after doing some more investigation i have to say that i tend to agree.

Although some may say that having multiple return statements in a method can create a confusion, and it will be hard to see all the exit places. But if this is the case, i would say that maybe the method needs to be re-factored and simplified?

Serialize POJO to XML

Today my colleague, Chandana was trying to convert POJO to an XML file. In our company we are working with dom4j library, but unfortunately it does not provide the capabilities to convert a Java object on the fly to an XML.

Off course Chandana could always manually create new Document, root element, sub elements and attributes and that would work fine. But this still does not solves Chandana’s problem where she needed to generate XML on the fly.

After some time doing a research on the Internet she came across this very nice library call XStream. Its a very nice library which allows to serialize objects to XML and back again. It doesn’t require mapping nor modifications to the object itself. They also have a short tutorial which converts a POJO to an XML in few simple steps. Very nice and usefuL