zondag 26 oktober 2014

Execution Framework introduction

Introduction:

In the execution service there are many ways to do a trick I have been googling around and found a lot of words but none of them explains well what is what and why.

Solvation:

Most of the executor service are obtained through the Executors class like:
 Executors.newCachedThreadPool(); 
This threadpool provides you with the number of threads that you need. It reuses threads that come available again.
Executors.newFixedThreadPool(10); 
This threadpool only provides you with the number of threads that you have declared 10 in my example.
Executors.newSingleThreadExecutor(); 
This threadpool contains one single thread and all given tasks will be executed in sequence.

Except ofr the SingleThreadExecutor() all of the others deal the same way if it becomes to lists. As soon you expose a list to one of the named threadpools the behaviour is as follows:

Lets say we take a CachedThreadPool or a FixedThreadPool we expose a list of elements to the executorservice initialized with one of the above named pools.  The first element of that is exposed for threading it will be spread over all the available threads. As soon the task is done, the next element of the list will use all of the threads.  This is a good thing if that is what you desire. But what if you want to share all the elements of the list shared over different threads. In other words every element of the list should be worked on at the same moment in parallel.

This is where the only pool comes in that can do that is the ForkJoinPool (since java 7).  This pool is not obtained through the Executors class but is instanciated on its own like :

ForkJoinPool forkJoinPool = new ForkJoinPool(3); The constructor takes a int as argument to initialize the number of threads, 3 in my example. The ForkJoinPool uses the  work-stealing philosophy. This means that if a thread is running out of jobs, it will visit one of the neighbor threads and see if he can help him out.

Conclusion:

If you want to work a single task in different threads the ExecutionService is what you need. If you need different tasks work in paralel the same time, you should use the ForkJoinPool.

If you are using a version of Java lesser then 7, you can try this trick with google guava. I worked at this time with version 18.0 and that worked fine. Here is a piece of example code:

//Java class
ExecutorService executorService = Executors.newCachedThreadPool();
//Guava class
ExecutionList executionList = new ExecutionList();
//code to execute
executionList.add( new myRunnable(), executorService);
executionList.execute();

Have fun!

zaterdag 11 oktober 2014

reading method from stack


Introduction:

One of my colleagues came up with the idea to setup logging through AOP. I always encourage ideas like that. This might set you on the wrong foot. This blog is not about how to deal with AOP. This little part is about how to read the method name from the stack so it can be written to a log.

The difficulty came that certain classes should log the method name with a certain pattern and other method names not. This made it hard to solve with the patterns available in the Loggger settings.

The solution:

The idea sounds simple. I worked it a bit more out to be comply  to the open closed design way. Because what I learned in composite designing is that every object should interfere as less as possible with other objects. In other words, It should be as independent as possible. The only way to achieve this is to use reflection.

There we open another discussion when and where should we use reflection. The fist rule I is simple If there is another solution that is as valid use that one. If there is not another valid option then use reflection. So why reflection in this matter?

There are two ways to solve this:


  1. hardcode every method name also as a constant in the class that needs to log the method name.
  2. Use Reflection to get the name of the stack.   
So this how we solved it:

String methodname = Thread.currentThread().getStackTrace()[1].getMethodName();
Logger.info(methodName);


The .currentThread() is a static method from Thread from which you can get the stack called in that Thread. In my example there was only one thread. 
The 1 refers to the position of the method you desire from the stack. The further back you want the higher this number becomes. The only bummer in this story is that you cannot obtain the names that have been given to the parameters of the required method. The jvm creates new instances of them and name them  like arg0, arg1 etc...

Conclusion:

Knowing what you are doing is very important. It also shows everytime that things in Java are not that hard ones you understand the concepts. This is something you probably not gonne use a lot but when you need it, it is handy.

Have fun!