Multithreading in real world


Most of you already know about threads and it’s benefits. This article will help you to understand how thread are being used in real world applications and what are the best practices. Threads are primarily used to achieve asynchronous  behavior and multitasking.

Below are some common real life use cases where threads have been used effectively.

  1. Animation and Games.
  2. Most of the GUI tools like Swing uses thread to keep track of events generated from widgets.
  3. Internally Junit uses threads for executing test cases parallel.
  4. Most of the web servers uses multithreading to entertain HTTP Requests parallel

Below are some common best practices for developing concurrent (multi threaded )applications.

Naming a Thread-

Thread should be named for easier debugging.  You can see in Eclipse that threads running in parallel with some random names like “Thread-124362563”. Giving a unique name to a thread will make it easier to identify the thread while debugging.

How many threads-

Before deciding number of threads to create , please keep in mind following facts.

  1. One processor executes a thread at a time and keeps switching between threads. Too much of switching can be an inefficient solution.
  2. Now a days most of the computers are multi core i.e comes with multiple processor so multiple threads can be executed simultaneously.

For long running threads one practice  can be to have no of processors as no of threads.

Use Immutable Classes 

Immutable classes, once created cannot be modified so one does not need to worry about sharing the state with multiple threads. This reduces  a great scope of synchronization.

Threads VS JMS –

I have developers discussing JMS VS Threads for developing asynchronous behavior.  I think JMS is more controllable way to use threads. JMS gives you additional features like tracking of request and retry to make a better enterprise choice.

Use Executors instead of Threads API

Object pool is a concept to avoid cost of object creation and destruction. JDK provides “Executors” as managed thread pool.

Use  java.util.concurrent

Java 5 added a java.util.concurrent  to the Java. This package contains a set of classes that makes it easier to develop concurrent scalable applications .Before this package was added , one need to use “synchronized” , “volatile” , “join” , “wait” and “notify” to write concurrent ( multi threaded applications) .

Popular items of “java.util.concurrent” are “BlockingQueue” , “semaphore” , “Lock Interface”  , “CountDown Latch” and “CyclicBarrier”.

Giving some details about Locks , CountDown Latch and Cyclic Barrier.

Locks

Prior to java 5  “synchronized” and “volatile” were the means of achieving concurrency. “Synchronized” provides a clean way to code but comes with limitation like try lock i.e. acquire only if lock is available. For more scalable concurrent solution JDK added lock interface and various implementations. “Reentrant lock” is a popular implementation of Lock interface.

Synchronized acquired a intrinitc lock on object wihis realeased automaticaaly. Bot with Lock , one need to release a lock programiticaaly. Best practice is to relase the lock in try block.

Synchronized acquired a intrinitc lock on object wihis realeased automaticaaly. Bot with Lock , one need to release a lock programiticaaly. Best practice is to relase the lock in try block.

CyclicBarrier and CountDown Latch

Both CyclicBarrier and CountDown latch are used for thread synchronization i.e. thread waits for another thread to complete their job at some point. Difference between both is that Cyclic Barrier instance can be reused while CountDownLatch instance can not.

 

 

 

 

 

 

Advertisements

Designing Modern Enterprise Search Solution –Part 1


This article would be a two part series.

  1. Part 1 – 5 requirements that will force you to adopt an enterprise search engine.
  2. Part 2– Addressing Enterprise search requirements with open source search platforms.

 

Part 1 – 5 requirements that will force you to adopt an enterprise search engine.

  1. Search over diverse content sources- An enterprise may need a search over diverse content sources including databases, document store, emails, JIRA, SharePoint and content management systems. A smart search solution should be capable enough to crawl these sources and make content searchable.
  2. Modern Search features- Now a days google has become benchmark for advance search features like Autosuggest, Did you mean “AAA”.
  3. Support powerful UI- To build a powerful search interface faceting (filters for search results) is must. I would be better if search engine provide responses in JSON so that
  4. Performance- For e.g. most of the searches below 100 ms.
  5. Full text search- With full text, search engine just do not match user specified term with 100% precision like “SQL like”. A full text search gives you flexibility to tune down the precision for better recall. Three full text realted concepta are  tokenization, stemming and ranking. Tokenization is Splitting a text into words, phrases and tokens. Generally it is done based on language grammar.

 

 

 

 

5 scenarios where you can bet for EJB3


I have most of the times worked on spring based projects. Recently I got a chance to design business layer based on EJB 3.

Below are the observations based on my experience.Any architect may consider EJB 3 , for these aspects.

  1. JMS Consumers: When you need to develop JMS consumers: EJB supported MDB are simplest JMS consumers having annotated transaction support.
  2. Asynchronous Communication: In EJB 3.1, session beans support asynchronous method invocations. Bean methods annotated with @Asynchronous are invoked asynchronously. When a client invokes methods with the @Asynchronous annotation, the container immediately returns control to the client and invokes the method in a different thread. The method may return a future object to allow the client to check on the status of the method invocation, and retrieve result values that are asynchronously produced. EJB 3.1 supports this feature by using Future, which is part of standard java concurrency. Future represents the result of an asynchronous computation. It supports both model of communication:,
    1. Retrieve-result-later asynchronous methods having Future<?> return type.
    2. Fire-and-forget asynchronous methods having void return type
  3. Multi node deployment
  4. Distributed Application: EJB3 support for remoting and simple distributed transaction support makes spring first choice for distributed applications.
  5. State-full Behavior

 

You can achieve all the features with other frameworks as well, but one need to develop custom components or integrate other frameworks, while these features are embedded in EJB 3 and can be applied easily.

Aspects of Web application to be secured


To ensure security of a web application is an important requirement now days.  One needs to first do a vulnerability assessment.

For example:

  1. If there is file upload feature, Application can be vulnerable for uploading viruses.
  2. If there is form submission, Application can be vulnerable for html/js injection.
  3. If plain JDBC has been used, there can be threat for sql injection.

One practice can be to setup some design and development guidelines to ensure that application is secure to at least common vulnerabilities. Please read more at http://makesecurejava.blogspot.in/