Hi, my name is Timo Ernst and I am a web expert.

New project: zkexamples.com

Posted on: May 17th, 2011 by Timo

I created a new online platform called zkexamples.com.

The goal is to provide really simple and quick examples for common ZK use-cases and requirements (what is ZK?).

The idea is based on flexexamples.com which was a great help to me when I was coding a lot of Adobe Flex.

If anyone has some nice examples to share, feel free to sign up and just post it (requires review and approval by me).

I, personally, try to post every time I learned something new about ZK.
Might help someone else who has the same problem.

TwitterDiggFacebookShare

Quickfix: Hibernate integration for ZK applications

Posted on: April 24th, 2011 by Timo

If you’re using the awesome ZK framework together with Hibernate you might run into some issues where Hibernate behaves a little funky while the application runs.

The trouble’s cause

According to the developers of ZK, the reason for this is ZK’s multi-threaded event model. Whenever an event gets fired, a new event thread is created. This will lead to multiple instances of the Hibernate session class (one per thread) and thus lead to unpredictable behavior.

The quick fix

If you don’t want to read the whole blah-blah in the linked article, here is the quick fix. Just insert the following lines into your zk.xml file and you should be fine:

<!-- Hibernate SessionFactory life cycle -->
<listener>
<description>Hibernate SessionFactory life cycle</description>
<listener-class>org.zkoss.zkplus.hibernate.HibernateSessionFactoryListener</listener-class>
</listener>

<!-- Hibernate OpenSessionInView life cycle -->
<listener>
<description>Hibernate Open Session In View life cycle</description>
<listener-class>org.zkoss.zkplus.hibernate.OpenSessionInViewListener</listener-class>
</listener>
TwitterDiggFacebookShare

Vector vs. ArrayList in multi-threaded Java applications

Posted on: April 20th, 2011 by Timo

I lately had to play around a lot with collections in multi-threaded environments and learned several new stuff that I’d like to share:

Vector vs. ArrayList

Where is the difference between both? When should you use ArrayList and when is the time to use Vector?
This is a very common question and often leads to huge discussions.
For me, the following two points are sufficient in order to decide when to use what:

  1. Vector is threadsafe. ArrayList is not.
    That means that you can have two threads accessing (and manipulating) a vector without having to worry about mutual exclusion.
  2. ArrayList is a little faster than Vector but not threadsafe.

Lists in multi-threaded environments

Now, if you have multiple threads in your application which both access collections at the same time, you might run into issues because this is simply not allowed. Think of the list as a piece of cake. Only one guy can eat it.

As mentioned above, Vector is threadsafe, so in theory you could assume that it’s perfectly alright if you only use Vectors through the whole application. That’s wrong. See the example below, which will cause a ConcurrentModificationException.

So, if you have thread 1 doing this:

List<Person> persons = new Vector<Person>();
fillList(persons);

for (Person person : persons){
	if (person.getName().equals("timo")){
		doSomething(person);
		persons.remove(person);
		break;
	}
}

.. and thread 2 does the same thing at the same time, you’ll most likely face a ConcurrentModificationException.

That’s because

  1. The “for (Person person : persons)” syntax creates an implicit iterator which is not thread safe.
  2. You cannot iterate over a collection and modify it at the same time.

Solution: Use the synchronized keyword and iterate over a copy but modify the original

List<Person> persons = new Vector<Person>();
fillList(persons);

synchronized(persons){
	List<Person> copy = new Vector<Person>(persons);
	for (Person person : copy){
		if (person.getName().equals("timo")){
			doSomething(person);
			persons.remove(person);
			break;
		}
	}
}

Wrapping the for loop into a synchronized block will synchronize access between multiple threads.
This must be done in all threads which could possibly manipulate the vector. Adding the synchronized keyword in just thread 1 will not work unless thread 2 also uses this keyword.
Also, the above code creates a copy of the original list and iterates over it. Note that the call of persons.remove(person) happens on the orignal list.

TwitterDiggFacebookShare

Japan will blossom again

Posted on: March 16th, 2011 by Timo
Japan Tribute

Being half japanese, I currently cannot think about anything else than the crisis happening in one of the most beautiful countries in the world. Sitting here in Germany unable to do much more than just watch news and refresh live tickers makes me go crazy.

Not only have several earthquakes and a huge tsunami taken thousand of lifes, now also a nuclear disaster seems to threaten the people of Japan including some of my relatives living in Tokio.

I can only hope that the 50 brave technicians left behind in Fukushima Daiichi find a solution for the problem to save the beautiful landscape and the ocean where I used to play and fish when I was a little kid.

From what I know, the people who suffered from the earthquake and the tsunami run out of food, electricity and most important: clean water.
Japan’s government today officially asked for help from foreign countries because they run out of resources to help the inhabitants who lost their homes and now must live in temporarily built camps.

So, here is one of the few things I can do (inspired by Dryicons):

Please, if you can spare
a few bucks, donate!

For donators from Germany: Deutsches Rotes Kreuz

TwitterDiggFacebookShare

Why everything seems to be just a little bit easier on a Mac

Posted on: January 30th, 2011 by Timo
Mac vs Win: Uninstall instructions

I just found these uninstall instructions for DoubleTwist on the net.
It was so funny, I had to take a screenshot of it

TwitterDiggFacebookShare