Wednesday, June 27, 2012

One obviously knows a lot of different methods to implement singleton in java. Singleton by definition means one object per classloader. Starting Java 5 we now have enums which are first class singleton objects. The best method in java for implementing singletons is via enum(s).

Lets take a look with an example of a DB connection pool manager. We're using Data source to get connections:

public enum DBConnectionManager {
INSTANCE;

private DataSource ds = null;
private Lock connectionLock = new ReentrantLock();

DBConnectionManager() {
try {
final Context initCtx = new InitialContext();
final Context envCtx = (Context) initCtx.lookup("java:comp/env");
ds = (DataSource) envCtx.lookup("jdbc/DBConnectionManager");
} catch (NamingException e) {
e.printStackTrace();
}
}

public Connection getConnection() throws SQLException {
if(ds == null) return null;

Connection conn = null;
connectionLock.lock();
try {
conn = ds.getConnection();
} finally {
connectionLock.unlock();
}

return conn;
}
}


Since we are using connection pooling, we do not really need to close the connection and it's the responsibility of the calling code to close the connection (which actually does nothing but returns the connection back to the pool).

Wednesday, June 6, 2012



Get a list of all the running Threads in the current JVM and its origination


Sometimes, it makes sense to know the list of all running threads in the JVM. Either when you are creating your own profiling tool, or, when you need to monitor the health of your system and need meta-information of your platform. You may also need the information of all running threads when you would like to selectively, say 'stop' the threads (a technique i would showcase in my subsequent posts).


There are two ways to get it:

1. The long way:


Get the root thread group. and then call the enumerate() function on the root group repeatedly.


ThreadGroup rootGroup = Thread.currentThread( ).getThreadGroup( );
ThreadGroup parentGroup;
while ( ( parentGroup = rootGroup.getParent() ) != null ) {
rootGroup = parentGroup;
}



Thread[] threads = new Thread[ rootGroup.activeCount() ];
while ( rootGroup.enumerate( threads, true ) == threads.length ) {
threads = new Thread[ threads.length * 2 ];
}

for(Thread t : threads)
{
System.out.println("Thread name:"+t.getName());
StackTraceElement[] stackTraceElements = t.getStackTrace();
for(StackTraceElement s: stackTraceElements)
{
System.out.println("class name="+s.getClassName()+" and method name="+s.getMethodName()+" at line no.="+s.getLineNumber() );
}
}


2. The short way:


Set threadSet = Thread.getAllStackTraces().keySet();
//now just for displaying get the threads
threads = threadSet.toArray(new Thread[threadSet.size()]);
for(Thread t : threads)
{
System.out.println("Thread name:"+t.getName());
StackTraceElement[] stackTraceElements = t.getStackTrace();
for(StackTraceElement s: stackTraceElements)
{
System.out.println("class name="+s.getClassName()+" and method name="+s.getMethodName()+" at line no.="+s.getLineNumber() );
}
}