Monday, November 22, 2010
Friday, November 19, 2010
Unsafe Singleton
Many mighy think, is there something called unsafe singleton? Singleton is the property, which lets just only one object floats at a time and there are no more than one object. This singleton program will create more than one object under multi threaded environment, which is totally against rationale.
public class SingletonUnsafeDemo
{
public static void main( String[] args )
{
Thread t1 = new Thread()
{
public void run()
{
System.out.println( "UnsafeSingleton: " + UnsafeSingleton.getInstance() );
}
};
Thread t2 = new Thread()
{
public void run()
{
System.out.println( "UnsafeSingleton: " + UnsafeSingleton.getInstance() );
}
};
t1.start();
t2.start();
}
}
class UnsafeSingleton
{
static UnsafeSingleton instance;
public static UnsafeSingleton getInstance()
{
if ( instance == null )
{
Thread.yield(); // !!
instance = new UnsafeSingleton();
}
return instance;
}
}
Tuesday, August 10, 2010
Round VI
Round V
Round IV
Round III
Labels:
decorator,
dependency injection,
LRU cache,
volatile
Round II
synchronized methods vs synchronized blocks.
Thursday, July 15, 2010
My CSC Rendezvous....
Wednesday, June 9, 2010
Absolutely Singleton.....
Many of Java developers know about singleton pattern and how to create it. But, creating a thread safe Singleton is the real absolute singleton. Otherwise, there are chances for ending up with different objects for different threads, which is totally against singleton.
A sample program given in the discusion here clearly explains how a singleton which is not thread safe can create different objects w.r.t different threads.
A sample program given in the discusion here clearly explains how a singleton which is not thread safe can create different objects w.r.t different threads.
Saturday, May 8, 2010
Servlet, JSP..
Saturday, March 27, 2010
Unix Questions
Adding Questions
Updated on 8th May 2010
Saturday, February 20, 2010
Interesting Questions....
Friday, February 19, 2010
JSP:Can a method be declared in scriptlets?
Can we declare a method in scriptlets? If so, what is the use of declaration tag?
If you want the answers, just Click here
If you want the answers, just Click here
Tuesday, November 10, 2009
Java Tasks
Some questions to ponder over,
Write a program to get the first two integers in a array which sums up to a given value. An integer array is given and a sum value is given. You have to find the first two numbers in the array which can add to give given sum value. Example: Given array is {10,34,56,80,1,36,93,62,23,90,21} and sum is 155. In this case, 93 and 62 will be the values adding upto 155.
Write a program to reverse a string without using string buffer and string builder. Example " my name is vinod " should be reversed to "ym eman si doniv"
Write a boolean isChild(String parent, String child) method in java, which returns true if a child string is in parent string in the same order else false.
Example1- parent1 - LOCAL,
child1 - LOL
returns true
paren1 - CHANGE,
child1 - AEC
returns false
Example1- parent1 - LOCAL,
child1 - LOL
returns true
paren1 - CHANGE,
child1 - AEC
returns false
Labels:
java programs,
reversing string,
string tasks in java,
strings
Subscribe to:
Posts (Atom)