Concurrent != Parallel

It’s easy to mix up concurrency and parallelism. After all both terms relate to the ability to run and progress multiple threads. There’s is a subtle distinction though:

Threads which run concurrently do not always run simultaneously (eg. 2 threads on 1 core)… although to the end user they may well appear to – provided the cpu switches between them quickly enough.

Threads running in parallel do run simultaneously, eg. 2 threads on 2 separate cores.

Why is this important ? because the way objects are named can give a clue as to how they should be used. For instance:

– A ConcurrentModificationException can occur in a single threaded process.

   Map<String, Integer> m  = new HashMap<String, Integer>();
   for (int i =0;i<1000;i++){
      m.put(String.valueOf(i), i) ;
    }
    for (Map.Entry entry:m.entrySet()){
      if (Math.random()<0.5) {
         //pretty much guaranteed to throw a ConcurrentModificationException
         m.clear();
       } 
    }

– The Concurrent CMS garbage collector will run (mostly) concurrently with the application threads. On the other hand the Parallel GC uses multiple threads simultaneously, none of which will run concurrently with the user threads (they effectively stop the application world)

Advertisements

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