Experience with Generics and ScheduledThreadPoolExecutor in Java 5

I got a chance to use generics recently. We are building a J2EE application that will work as a server for another client product. As all other server, we needed a Scheduler to schedule various background tasks. I thought I will do it and with Java 5 it is kind of straight forward with ScheduledThreadPoolExecutor. To add more fun to it, I added few more features and there I ran into problems with Generics.

  1. The biggest problem is very basic. Generics are compile-time constructs and hence the power of generics is greatly limited. There are reflection APIs but they are of no use as I can’t use them to do anything functional.
  2. The code becomes very difficult to understand.
  3. The classes that uses parameters are not good candidates for serialization and de-serialization. There is no way to push the types while de-serializing and this is again due to compile-time nature of it.

Looking at all above, I decided the cut the scope of generics in my module and its pretty much managiable.

About ScheduledThreadPoolExecutor, its a great funtionality. I just wanted that there is way to relate the task that is submitted and Runnable that is created by the executor. This is required in many utility methods like RejectedTaskHandler, where in I might want to invoke a funtion on the rejected task. There could be lot more applications for this. In Sun’s implementation I found out that it basically uses the returned ‘Future’ object only as runnable, but I cannot write code on the knowledge of internal implementation. It can change anytime and my program will break.


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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: