Java Concurrency Synchronizers

November 5, 2008

Java Concurrency Utilities provides a number of powerful and high-performance threading utiities.  At the high level they can be grouped into four categories and this article will cover one of the categories.

  1. Thread Pools and Task Scheduling
  2. Concurrent Collections
  3. Locks and Synchronizers
  4. Atomic Variables

We all know that Java supports synchronization since day one through the synchronized keyword, but the limitation is that this mechanism works at the block level and is limited to a single thread at a time.  A number of new mechanisms were introduced.  Among them are semaphore, barrier, latch and exchanger.

Semaphore are used to control or limit the number of activities that can access a certain resource or perform a given action at the same time.  An easier way to understanding and remembering what a Semaphore is by associating Semaphore with permits.  A semaphore maintains a set of permits and a thread must acquire a permit from the semaphore before it can obtain a resource or perform a certain activity.  The permit is returned to the semaphore when thread is done accessing a resource or perform a certain activity.  If all the permits were already given out, then the next thread that asks fro a permit will be blocked.

Latch is used to allow one or more thread to wait for a set of threads to complete an action.  Once a latch is set, it never changes.  Latch is commonly used to coordinate certain threads and the common use case for latch is start several threads and have them wait until a signal is received from a coordinating thread.  Another example in a multiplayer games is you don’t want the game to start until all the players have joined.

CyclicBarrier is used to create a barrier and there are two different kinds of barrier.  The first kind is a barrier with a number of threads and the other is a number of threads and a barrier action.  The barrier action is a Runnable task that runs when all the threads have joined together.  Basically a barrier is used stop a set of threads from running until they all reach a specified point.  Comparing to a latch, which is used to let threads run wild, a barrier is used stop a set of thread.

Out of the four kinds of synchronizers, exchanger is a unique one.  It is used to allow two threads to exchange data in thread-safe manner.  Imagine the producer and consumer problem,  an exchanger can be used to allow producer and consumer to exhange the buffer that contains tasks to do in one shot, instead of consumer picks one task out of the task queue at a time.

Next article will cover Atomic Variables.


Performance Tuning Lessions

July 31, 2008

Depends on how you look at it, performance tuning can be a stressful task or a really fun and challenging adventure.  Just like anything else, to be good at it you have to at least gone through the performance tuning process once, but preferably twice.

One of the things that can greatly help you is to learn some commonly performance tuning techniques or even better yet is to learn from someone who just finished doing performance tuning.  Well luck has it, a recent article at the Architect Zone at DZone site talks about what Jeroen Borgers had gone through and he was generous to share with us his performance tuning adventure.

The takeaways for after reading this article are:

  • When some operation is expensive, do it less often and if that is not possible, do it in batch or asynchronous.
  • For a web application that involves a database, ensure appropriate indexes are created and tailored to the data access pattern based on business use cases.

Have fun performance tuning and please share your performance tuning techniques.


Presenting At 360|Flex Conference in San Jose 8/18-8/20

May 15, 2008

I am pretty excited about the opportunity to present at the upcoming 360|Flex conference in San Jose from August 18 to 20. My coworker and I will give a talk about our experiences and what we had learned from developing an internal SOA application at Yahoo!. This application uses Flex and a host of Java technologies on the server side. Come and check us out in August in San Jose.


Understanding Spring Framework Hierarchical Context

May 15, 2008

Not many people are familiar with the support that Spring framework has for hierarchical context. The question is why do we need such feature. This feature is useful in the scenario where your product has multiple web applications and all of them depend on a common framework or library. Instead of deploy that framework or library in each web application, you can deploy it in the Tomcat top level class loader, which affectively makes the framework or library available to all web applications deployed in that Tomcat server.

This is when hierarchical context feature comes to the rescue. A very good write up about this feature is at this blog.


JavaOne Week – CommunityOne Summary

May 7, 2008

JavaOne week was kicked off with CommunityOne event on Monday 5/5.  If you didn’t attend, you missed a free event that was packed with interesting session.

It seems like Sun is open up in a big way in terms of creating various open sources projects, supporting various popular scripting languages, supporting startups, and embracing open source in a big way.  I think that is great, but I wonder how they will make money from it.

One of the tracks at CommunityOne is NetBeans.  I must say NetBeans has improved a lot and I am impressed with recent features that were added.  I got my hands dirty with NetBean 6.1 and surprised its speed and rich feature set like UML, scripting languages support like Ruby and Rails, PHP and Python (in the near future).   Highly recommend that you check out the latest feature of NetBeans.  The folks at Sun created a layer called GSF services to allow NetBean to support future languages easily.  I saw a demo of developing PHP in NetBeans and it was great.  All the usual convenient features like code completion, debugging and deployment are there in NetBeans.  This should make a lot of PHP developers happy.

Who would think that Sun is working on a soon to be an open source game server.  I attended a presentation about project Darkstart – game server and learned the interesting challenges that they are solving for their massive and multiple online game server.  They combine a lot of techniques from various disciplines like enterprise development, distributed system to solve the scaling and failover issues.  The main goal is to allow developer to write online games that can scale easily, but without having write code to deal with scalability.

Another interesting session that I attended was about another Sun’s open source project call SocialSite, which basically is an implementation of the OpenSocial specification and more.   I think this is great.  In the near future it will be easy to integrate the social networking features into a Java web-based application.  I am intending to keep an eye on this project and possibly would like to contribute.

Stay tune for my report on JavaOne.


Being A Productive Java Developer

March 7, 2008

A highly productive Java developer will often figure out the right tool to use for the right task. If you are a Java developer and you use Eclipse IDE, then you are on your way to being productive.

One of the things about Eclipse is that it can suck up your computer memory and you may encounter ‘OutOfMemoryError’. To give Eclipse more memory to use, you may want to add the following to eclipse.ini file.

-vmargs -Xms512M -Xmx1024M -XX:PermSize=128M -XX:MaxPermSize=256M

To verify the settings are properly configured go to menu Help->About Eclipse Platform and then click on the ‘Configuration Details’ button.

For more info. see this article on Eclipse Zone.


Cool Google Collections Library

February 21, 2008

I stumbled across an article on TSS.com about Google Collections Library and I must say I like what it offers. Mainly because

  1. You end up writing less code
  2. Your codes will be cleaner
  3. Your codes will have less bug
  4. More time to write more code

You can read more about this library at http://www.javalobby.org/articles/google-collections/


Ten Things Java Programmers Should Know About Python

January 6, 2008

In recent years, scripting languages are used more and more for developing web sites and web applications. Among the popular scripting languages is Python. If you are a Java developer and is looking to learn what Python is all about, this post is for you.

  1. Python is a general programming language invented by by Guido van Rossum back in 1990.
  2. It is a very high level, dynamic, objected oriented and cross-platform language.
  3. Described by one of the authors of the book “Python Cookbook” as a language with syntactic ease of Rexx, the semantic simplicity of Tcl, the intellectual rigor of Scheme, and the awesome power of Perl.
  4. Python is more than a language, it has a set of standard library and other extension modules. This is similar to Java language.
  5. There are two production-quality implementations: CPython and Jython. Python .NET is an experimental implementation.
  6. Python does not use braces or begin/end delimiters around blocks of statements. Indentation is the only way to indicate such blocks.
  7. Some of the interesting keywords are: lambda and yield.
  8. One of the built-in number objects is complex numbers.
  9. The built-in type None denotes a null object.
  10. Python is an OO language, but it doesn’t force you to use OO paradigm. It also support procedural programming paradigm with modules and functions.

You can find a lot of information, tutorials, about Python at http://www.python.org/doc/. There is a free Python book at http://diveintopython.org/.

Have fun.


Servlet Filter Came To The Rescue

December 29, 2007

In my previous post I outlined a couple of limitations that Flex HTTPService class has in dealing with Restful style web services. After giving it some thought, I came up with a solution that will allow me to continue to use Flex with Restful style web services without changes to the backend. The core of the solution is the idea of intercepting the incoming request on the server side using Servlet filter and use the Decorator pattern to adapt the needs of the service Restful web services. The solution consists of the following parts:

  1. Servlet filter to intercept incoming Restful style web service requests
  2. Develop a custom HttpServletRequest wrapper class that converts information like method name or header from the request parameters into right format that the server side web service handlers require.
  3. Develop a custom HttpServletResponse wrapper to convert all status code to 200 so flex will be able to get to response body

The above solution is very specific to my application and the willingness to bend some rules in making Restful web services calls from the Flex front end. Basically each web service request will append a request parameter with an agreed upon name between client and the server. Only when this special request parameter is present, the the Servlet filter will trigger the usage of request wrapper and response wrapper. The other issue with status code is since my Restful web service APIs return an error message in an XML structure in the response body, which allows the Flex client to find out what’s wrong.

With this solution, I am able to continue to Flex to develop my application UI.

Happy flexing everyone.


How Does Spring Bean Container Dispose Your Beans?

February 23, 2007

Spring bean container provides a powerful and flexible way to manage Spring bean’s lifecycle. If a Spring bean needs to clean itself up when Spring bean container is gracefully shutting down, it has several ways of specifying that intent. The intrusive and not recommended way is to implement the DisposableBean interface. A better way is to specify a call back method in the bean’s definition using the ‘destroy-method’ attribute. Spring 2.0’s convention over configuration model provides a very convenient way to eliminate the need to specify a call back method at the individual bean level. This new feature allows you to specify a convention at the application level using the ‘default-destroy-method’ attribute of the ‘beans’ XML element and if needed this convention can be overridden at the individual bean level.

So the question is how does Spring bean container knows when to shut itself down? The answer is it depends (I bet you heard that before). A Spring bean container usually is used in two environments: web-based application or standalone Java application. If it is the latter environment, then your application’s initialization step must call the method ‘registerShutdownHook’ of your choice of ApplicationContext implementation (BTW, method ‘registerShutdownHook’ is in the AbstractApplicationContext class). Then you may ask what exactly does the method ‘registerShutdownHook’ do? Apparently the Java JVM has a facility for a Java application to register a call back thread such that this thread will be given a chance to run during the JMV shutdown sequence. So method ‘registerShutdownHook’ calls ‘Runtime.addShutdownHook’ method and passes in anonymous class that extends Thread class and the ‘run’ method implementation contains a single call to a Spring bean container method to perform context closing, which publishes a ‘ContextClosedEvent’ and cleans up the singletons cache. During the singleton cache cleanup process is when Spring bean container will call each singleton bean’s cleanup call back method.

What’s about when a Spring bean container is used in web-based application environment? Does it have to call the same ‘registerShutdownHook’ method? Well, as it turns out, Spring bean container leverages the servlet context call back mechanism through the ‘ServletContextListener’ interface. Typically a Spring powered web-based application uses ‘ContextLoaderListener’ to bootstrap Spring’s root WebApplicationContext and ‘ContextLoaderListener’ implements ServletContextListener interface.

So now you understand how Spring bean container knows when to call your bean’s clean up method, my question is have you ever developed a Spring bean that needs to perform some sort of clean up? If so, what’s does your bean need to clean up?