Wednesday, December 31, 2008

Sharing variable between threads

I take no credit for what I am going to write here, It's the result of reading few good books on Java Concurrency.

Lots of programmers think that synchronization is to be used when multiple threads are modifying a shared variable. What about a situation when we know that only one particular thread will modify the shared variable and all others will only read that variable. Should we be using synchronization?
Consider following example and think about the output:
public class Test {
private static boolean ready;
private static int number;

private static class ReaderThread extends Thread {
public void run(){
while(!ready){
Thread.yield();
}
System.out.println(number);
}
}
public static void main(String[] args)throws Exception {
new ReaderThread().start();
number=42;
ready = true;
}
}

I had my answer 42, its got to be 42. But its not always true. Its quite possible that thread does not see the value of ready and get stuck in infinite loop.
When you run java process with '-server' option, according to JVM specs, JVM can do some optimization. This optimization involves, keeping the value of the local thread specific variables in local registers or caches and update the memory location at will. So unless JVM knows that a particular variable is shared between threads, it can apply some optimizations. If you run the process with '-client' option, JVM does not apply any optimizations and hence you will always see the result as you would expect. Even in production environment, things like above are hard to catch and sometime it can run correctly for its entire life.
In above example, there is no hint to JVM to know that 'ready' is shared, so it can opt to keep the changes in variable in local cache and hence other threads can see the stale value.
So whenever there is any sharing use either final or volatile or synchronization.
Volatile forces JVM to update the shared memory location with each update on the variable and hence all the threads will see the updates value. Similarly with use of synchronization, JVM knows that it has to update the memory location for each update in shared variable.

Sunday, August 24, 2008

few things to remember while using logging frameworks

Use of debug/info etc methods from any logging framework (like log4j) helps you to get important information from your application and helps you analyze problems in the application.

It's helpful to use logging messages as much as possible keeping in mind the information you might need while analyzing some problems. But also there are a few things, one should keep in mind while using them.

Take an example,
List aList = new ArrayList();
//do some operations on list

//print the list contents
log.debug(aList.toString());

You might think that you have used debug method so this rather not so important information will not get printed when log level is more than debug. But, do consider the size of the list. No matter what the debug level is, the toString method will always get executed. In your production environment, though the content of the list will not get printed, but the toString method will always get executed, and depending upon the size of the list, it might take considerable amount of cpu time.

I such cases, one can check the log level and then call the methods, hence triggers only an integer comparison.
if(log.level <= debug)
log.debug(aList.toString());

Friday, July 25, 2008

concurrency in java - I

Concurrency in Java is easy, but it's tricky. If you don't use it wisely, you will end up in a mess. The other important thing is to know about the features which Java provides out of the box.

Hash map is handy, we all use it. To use it in multi threaded applications Java provides synchronized hash map, which is a wrapper for hash map.
Synchronized hash map uses the lock on the whole map, which can impact performance when multiple threads access the map too often and spend time to acquire the lock. Data structure for map is a table with hashing and chaining. So, when one thread is trying to put a value in map, it would access one row (the whole chain in that row) in table. Locking only that particular row will help, but synchronized map will lock the whole map, hence block all other threads.
Java also provides concurrent hash map, which uses strip locking, which is , lock the row of the table and not the whole table. Hence multiple threads can access the map without any concurrency problem.

JMX - way to go

I knew next to nothing about JMX until recently, and then I had the opportunity to explore it. JMX can be used in building distributed application, building monitoring tools for applications.

With JMX lots of things get easier. I really do not intend to repeat the information which is available on internet and let me tell you, you will get plenty of good web-sites if you search on google. Instead I want to touch a few points which give good impression to a newbie who knows a little about JMX.

First of all, use of JMX is really easy. Starting with 1.5, JDK comes with JMX console Jconsole, which can be used to explore the mbeans (management beans, the beans which you register with mbean server).

To simplify matters further, there is Spring. Spring really makes it a lot easier to use JMX. A couple of configuration in xml and you are ready to see the JMX in action.

With JMX, you can see runtime information about your applications. You can expose the information through mbeans and can access them while your application is running, hence helps in monitoring.

Remember log4j, how it makes life so much easier. And remember when you face an issue in production environment and all you can do is, analyzing logs; but wait a minute, you realize that log levels are set to INFO or ERROR but you want to make them DEBUG. With log4j and JMX you can do this with a click of mouse.

You can go further to hook the JMX with snmp and let your existing snmp tool monitor your application with subagent talking to Mbean server.

Tomcat and many application servers also expose mbeans which you can use to know the insight of server.

I hope you felt a little excited about JMX. I do not intend to teach you JMX in this post, but if you wish, we can definitely discuss on this topic further.

Thursday, July 24, 2008

More Questions !!

What are the different type of references in Java ?

What is a ClassLoader, when will you need one, what are the different types of class loaders in Java ?

What are the various aspects of Java that are NOT Platform dependent ?

What is the difference between extending the Thread Class and implementing Runnable.. No the answer is not that you can extend only one class :-)

If you have a Static Method in Java that is Synchronized, how is it different from a Synchronized method of an Object ?

What is the Time Lag between starting a Thread and the actual running of the new Thread ?

Sunday, July 20, 2008

Encapsulation VS Abstraction VS Data Hiding

Most of the books don't really clearly differentiate between Encapsulation, abstraction and Data Hiding and most of the time people treat these terms as synonyms.
When we ask people whts encapsulation ... the answer given is "Code is written in such a manner so that it acts as a capsule. And just like a capsule where we dont know whts inside it similarly we have code and we use it without knowing worrying about whts inside."
Now if we carefully analyse then above definition is mixture of all three terms and its not just encapsulation as is being told.
Data Hiding clearly means we need to hide data but then its our choice wht we need to hide and expose and thats where abstraction comes into picture.

Abstraction refers to deciding what are necessary details which we need to include in our code and expose[just like creating an ADT where mandatory operations are exposed but implementation is hidden]. But the implementation of those details needs to be hidden.

Now encapsulation provides us a placeholder [i.e a set of variables and operations] which is ultimately needed to implement Data Hiding and Abstraction.

Thursday, June 26, 2008

Java Messaging service ...

It was just by chance i came across an article on JMS and never knew it can be so useful in client - server application.

JMS helps us in implementation of distributed systems with efficiency. Following diagram explains the architecture of JMS.

Many of the other advantages of JMS include abstraction and system scaling but then it can lead to some complications too.

You can go through following url for more details on JMS.
http://www.javaworld.com/javaworld/jw-10-2002/jw-1025-jms.html?page=1

Wednesday, June 25, 2008

Java Break - Interview Questions Section

I am putting some common and not so common Interview questions here (Only related to core Java). This is going to be a great effort, to compile and write list of questions, so allow me to take my time.
I will initially add only questions, and as and when i get some time, I will also put answers. However, others are welcome to add answers. Needless to say, correct me if i go off the track.

This is going to be an ongoing effort and I promise to make this list bigger and as relevant as possible. This is going to be a fun...

Difference between Interface and Abstract class.


here goes the answer


What is autoboxing.


What is the difference between wait and sleep.


Explain valoatile modifier. How is it different to synchronize keyword?


Explain final, finally and finalize.


Whats the difference between yield , wait and sleep?

Tuesday, June 24, 2008

Java 6 feature - Scripting in Java

Other day i was reading some of the new features introduced in Java6 and one of the feature is that now java supports scripting. Some of the advantages listed on different sites were, such as providing extensions to your Java application so that users can write their own scripts to extend or customize the core functionalities. Scripting languages are both simpler to understand and easier to write, so they can be ideal to give (technical) end users the possibility to tailor your product to their needs. But, is it really a great thing? I still dont find (probably I am too lazy to search more on this topic and I hate Scripting as much as I know they are hard to avoid) a good good reason to have this feature in Java. Anybody?

References in java

Everyone knows about strong references in Java, few know about weak references as well. I was recently preparing for an interview and I wanted to read some more about weak references and found a really nice article about different types of references in Java. Well I was looking for 2 and i found 2 more , strong, weak, soft and phantom reference.

A very good article explaining different references , worth giving a look:
http://weblogs.java.net/blog/enicholas/archive/2006/05/understanding_w.html

Monday, June 23, 2008

Size of objects in java

We all know that there is no operator or method to calculate size of an object in java. But can't really understand the reason for it, i mean if c++ can have it then why not java?
Only way to calculate appox. size of an objects might be to find out all the primitive data types forming the object and then add up their byte size.

If anybody knws a better a way or knws more size calculations then comments are welcome ...