I created this blog specially for myself to keep track of all the issues or problems for which I Googled a lot and then found the best possible solution.
Thursday, June 30, 2011
J2EE:SERVLET(How do you make a Servlet thread safe?)
Q : How do you make a Servlet thread safe? What do you need to be concerned about with storing data in Servlet instance fields?
A : A typical (or default) Servlet life cycle creates a single instance of each servlet and creates multiple threads to handle the service() method. The multithreading aids efficiency but the servlet code must be coded in a thread safe manner. The shared resources (e.g. instance variables, utility or helper objects etc) should be appropriately synchronized or should only use variables in a read-only manner. There are situations where synchronizing will not give you the expected results
as shown in the diagram below and to achieve the expected results you should store your values in a user session or store them as a hidden field values. Having large chunks of code in synchronized blocks in your service or doPost() methods can adversely affect performance and makes the code more complex.
Alternatively it is possible to have a single threaded model of a servlet by implementing the marker or null interface javax.servlet.SingleThreadedModel. The container will use one of the following approaches to ensure thread safety:
Instance pooling where container maintains a pool of servlets.
Sequential processing where new requests will wait while the current request is being processed.
Best practice: It is best practice to use multi-threading and stay away from the single threaded model of the servlet unless otherwise there is a compelling reason for it. Shared resources can be synchronized, used in readonly manner, or shared values can be stored in a session, as hidden fields or in database table. The single threaded model can adversely affect performance and hence has been deprecated in the servlet specification 2.4.
As shown in the diagram above, threads share the heap and have their own stack space (i.e. each thread has its own stack). This is how one thread’s invocation of a method (doGet(), doPost()) and its local variables (e.g. int y ) are kept thread safe from other threads. But the heap (e.g. int x ) is not thread-safe and must be synchronized for thread safety or stored in an HTTP session or stored as a hidden field. The variable “CONSTANT” is a read only immutable field since it is marked as final and hence thread-safe.
Wednesday, June 29, 2011
JAVA:Singleton Design Pattern
With the Singleton design pattern you can:
Below example shows how to create a singleton object. A singleton is a class of which there can only be one instance in the same Java Virtual Machine.
To create a singleton there has to be a private constructor because the class will itself control the one and only instance that will be created, and of course a private constructor cannot be called from outside the class.
Instead, a method is created with public access that returns the singleton instance (if the method is called the first time the object is instantiated). The example class MySingleton illustrates this:
/**
* MySingleton.java
*
* @author www.javadb.com
*/
public class MySingleton {
//the static singleton object
private static MySingleton theObject;
/**
* private constructor
*/
private MySingleton() {
}
/**
* Checks if the singleton object is created or not,
* if not it creates the object and then the object is
* returned.
*
* @return the singleton object
*/
public static MySingleton createMySingleton() {
if (theObject == null)
theObject = new MySingleton();
return theObject;
}
}
No matter how many times the method createMySingleton() is called, it will always return a reference to the same singleton object.
This code illustrates this by calling the method twice and then compare the two references. The output of the code is 'true' since they both point to the same singleton object.
- Ensure that only one instance of a class is created
- Provide a global point of access to the object
- Allow multiple instances in the future without affecting a singleton class's clients
Below example shows how to create a singleton object. A singleton is a class of which there can only be one instance in the same Java Virtual Machine.
To create a singleton there has to be a private constructor because the class will itself control the one and only instance that will be created, and of course a private constructor cannot be called from outside the class.
Instead, a method is created with public access that returns the singleton instance (if the method is called the first time the object is instantiated). The example class MySingleton illustrates this:
/**
* MySingleton.java
*
* @author www.javadb.com
*/
public class MySingleton {
//the static singleton object
private static MySingleton theObject;
/**
* private constructor
*/
private MySingleton() {
}
/**
* Checks if the singleton object is created or not,
* if not it creates the object and then the object is
* returned.
*
* @return the singleton object
*/
public static MySingleton createMySingleton() {
if (theObject == null)
theObject = new MySingleton();
return theObject;
}
}
No matter how many times the method createMySingleton() is called, it will always return a reference to the same singleton object.
This code illustrates this by calling the method twice and then compare the two references. The output of the code is 'true' since they both point to the same singleton object.
JAVA:Marker Interface (What,Why & How?)
The Purpose of the Marker Interface
One of the "clean" features of the Java programming language is that it mandates a separation between interfaces (pure behavior) and classes (state and behavior). Interfaces are used in Java to specify the behavior of derived classes.
How to create own marker interface in java?
Interface with no methods, which a class can implement to show that it has certain properties. So you can have
public interface Marker{ }
How JVM will treat that interface as Marker interface and do the required functionality?
JVM does nothing but mark the class as implementing the interface. You can then later check whether “objects instanceof Interface" and find out whether or not it is present. As in the case of java.io.Serializable, ObjectOutputStream does this job.
Please note you should avoid create new ones. Annotations introduce in Java 5 are a generic mechanism of adding metadata to a class.
Often you will come across interfaces in Java that have no behavior. In other words, they are just empty interface definitions. These are known as marker interfaces. Some examples of marker interfaces in the Java API include:
- java,lang.Cloneable - java,io.Serializable - java.util.EventListener
Marker interfaces are also called "tag" interfaces since they tag all the derived classes into a category based on their purpose. For example, all classes that implement the Cloneable interface can be cloned (i.e., the clone() method can be called on them). The Java compiler checks to make sure that if the clone() method is called on a class and the class implements the Cloneable interface. For example, consider the following call to the clone() method on an object o: SomeObject o = new SomeObject(); SomeObject ref = (SomeObject)(o.clone());
If the class SomeObject does not implement the interface Cloneable (and Cloneable is not implemented by any of the superclasses that SomeObject inherits from), the compiler will mark this line as an error. This is because the clone() method may only be called by objects of type "Cloneable." Hence, even though Cloneable is an empty interface, it serves an important purpose.How to create own marker interface in java?
Interface with no methods, which a class can implement to show that it has certain properties. So you can have
public interface Marker{ }
How JVM will treat that interface as Marker interface and do the required functionality?
JVM does nothing but mark the class as implementing the interface. You can then later check whether “objects instanceof Interface" and find out whether or not it is present. As in the case of java.io.Serializable, ObjectOutputStream does this job.
Please note you should avoid create new ones. Annotations introduce in Java 5 are a generic mechanism of adding metadata to a class.
Monday, June 27, 2011
Subscribe to:
Posts (Atom)