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.
Friday, July 25, 2008
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.
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 ?
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.
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.
Subscribe to:
Posts (Atom)