Home > Programming > Series about Java Concurrency – Pt. 1

Series about Java Concurrency – Pt. 1

As this is one of the things I’m currently occupied with, I’ve decided to start my blog with a series about Java-Concurrency. It’s going to consist of a series of thread related puzzles and their solutions. This approach is heavily inspired by the very interesting book Java Puzzlers which I strongly recommend to everyone who is doing serious Java programming. The solutions, together with detailed explanations, will usually appear a few days after the puzzles. You are strongly encouraged to analyze the source code carefully and predict its behavior without using a compiler. Only then you should run the program to see if your assumptions where true. Also note that because we are talking about threads, the results you get may vary depending on your hardware, operating system, JVM and system load.

So, what does the following program print (execute it at least 10 times)? Can you explain what is happening? If you think that you know what is going on, feel free to post it!

package at.lnet.puzzle;

import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;

public class AllTheSameButDifferent
{
    private static EntityManager entityManager;
    
    private static class EntityManager
    {
        private final Set<Integer> data = Collections.synchronizedSet(new TreeSet<Integer>());
        
        public void persist(final int value)
        {
            data.add(value);
        }
        
        public Set<Integer> getData()
        {
            return Collections.unmodifiableSet(data);
        }
    }
    
    private static class Action implements Runnable
    {
        private final int data;
        
        public Action(final int data)
        {
            this.data = data;
        }
        
        @Override
        public void run()
        {
            synchronized(this)
            {
                if(entityManager == null)
                    entityManager = new EntityManager();
            }
            entityManager.persist(data);
        }
    }
    
    public static void main(final String[] args) throws InterruptedException
    {
        final int NTHREADS = 4;
        Thread[] threads = new Thread[NTHREADS];
        
        for(int i = 0; i != NTHREADS; ++i)
            threads[i] = new Thread(new Action(i));
        for(int i = 0; i != NTHREADS; ++i)
            threads[i].start();
        for(int i = 0; i != NTHREADS; ++i)
            threads[i].join();
        
        System.out.println(entityManager.getData());
    }
}
Advertisements
Categories: Programming Tags: , ,
  1. May 20, 2011 at 04:57

    Each thread synchronizes on a different this, multiple EntityManagers are created and some data is lost.

  2. May 22, 2011 at 13:17

    Exactly! I’ve explained all this in detail in Series about Java Concurrency – Pt. 2.

  1. December 6, 2009 at 12:51
  2. May 10, 2011 at 22:59

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: