Thursday, August 12, 2010

What is a threadpool executor

Imagine you have a web application which processes a lot of requests. The requests could vary from fetching a file to inserting or recording values in a database. Assuming you have around a hundred requests calling your application in one second. In this scenario a likely design would be to spawn a thread for each incoming request which will execute the implementation logic.
However, this approach is bound to create problems as the number of threads would increase geometrically when scaling and at the same time each thread has it's own resources. This means additional resources which have to wait to get garbage collected at the mercy of the JVM.
This is where a threadpool executor helps. Typically you assign a fixed number of threads that will be 'pooled'. When your application has an incoming request a thread is allocated, which then goes on and allocates the implementation to the code and returns back to the pool. This reduces the number of threads that we use in our application and thereby conserve resources. This architecture helps in keeping manageable the number of threads in a multithreaded application.

Guidelines for effective use of thread pools:
  • Be careful when using pooled threads for potentially long-lived operations. If the program must wait for a resource, such as an I/O completion, specify a maximum wait time, and then fail or requeue the task for execution at a later time. This guarantees that eventually some progress will be made by freeing the thread for a task that might complete successfully.

  • Understand your tasks. To tune the thread pool size effectively, you need to understand the tasks that are being queued and what they are doing. Are they CPU-bound? Are they I/O-bound? Your answers will affect how you tune your application. If you have different classes of tasks with radically different characteristics, it may make sense to have multiple work queues for different types of tasks, so each pool can be tuned accordingly.


Posted from Blogium for iPhone

Monday, August 9, 2010

Synchronized and instantiated

This was a question my manager put across to me when I was in some discussion with him:
We have a class A which has a synchronized method say abc(). Two instances a1 and a2 are created from A.
Now two threads t1 and t2 call the abc() method on a1 and a2 respectively. The question is:
Will the execution of abc() be synchronized?
My answer was no. This is because the scope of abc() is the object and not the class. Hence when the same method is called on different instances, their scope is restricted to that instance and execution continues. Had the threads been executing the method on the same object, abc() would have been synchronous.
The solution to this is to declare the method as static. This keeps the scope of the method at the class level and would behave synchronously across multiple instances.

Posted from Blogium for iPhone

Wednesday, July 28, 2010

Usage of class and local variables

A class variable in Java is one whose scope is the complete class itself whereas a local variable is one whose scope is confined to the method enclosing it.

Generally, the two have the same name. The class variable is indicated by using the keyword 'this'. 'this' indicates that the variable being referred to belongs to this instance of the class.

An example of such an assignment would be:


class Hello{ (1)
String text = "Hello"; (2)

/*
*@This method sets the value of the String in the class
*/
public void setString(String text){ (3)
this.text = text; //Assign the value of the parameter String to the class variable. (4)
}

}
As is self evident, statement (4) will assign the value of a local variable to a class variable having the same name but differentiated by 'this' keyword.

There are different opinions on whether to use the same name for both variables. Some recommend using different names so as to avoid unambiguity. However, it is always a good practice to use the same name so as to not get too many variable names in the code. And since Java does provide a keyword 'this', it's always good to use it.
The only caution to be exercised is to be thorough with the concept of the 'this' keyword.

Recommended books for starters

Here are some of the books that are standard for Java beginners:
1. Java complete reference by Herbert Schildt
2. Head First Java from O'Reily
These two books will help a Java newbie to get started on Object Oriented concepts and how it differs from C++.

Posted from Blogium for iPhone