-- The thing to know here is that the context class loader is loading the class. All apps default, when first started, to the JVM application classloader as being the currentThread context loader. Often code like xerces, ejb based client code, etc will set a different classloader on the context thread. The main purpose is to allow higher up loaders in the hierarchy of a chain to "see" classes lower than it. Normall, any class loaded by a classloader can use any other class loaded by it or any of its parent (with regards to package names, visibility, and other java rules). But a parent loader can't "see" any child loaders classes. By using the thread context loader, a parent loader can be passed a reference to a child classloader of its own or any other classloader and ask that classloader to load classes for it, thus delegating in an unusual manner. It does, however, allow code loaded by the boot loader (rt.jar) to use your own code loaded by the app loader such that things like EJB calls are possible by rt.jar boot loaded classes. You may have to set the loader to your custom loader if you use custom loaders, otherwise its set to the application loader and by default any class loaded by the boot loader or extension loader can access the application loader ref through the context loader, allowing any code there to "see" any classes loaded by the app loader. This is how rt.jar EJB/rmi code can use your classes and reflection, even though they are loaded by a higher parent loader.
-- Sun Java Forum
Tuesday, March 23, 2010
Monday, March 22, 2010
Recursive Deletion on linux filesysem
find . -type d -name "CVS" -exec rm -rf {} \;
-- deletes all CVS directories starting from current dir.
-- deletes all CVS directories starting from current dir.
Saturday, March 20, 2010
Interfaces and Implementations
Suppose that you are building an
ABSTRACT FACTORY for the creation of shapes. This factory will be an interface and will be implemented by a concrete class. What should you name them? IShapeFactory and ShapeFactory? I prefer to leave interfaces unadorned. The preceding I, so common in today’s legacy wads, is a distraction at best and too much information at worst. I don’t want my users knowing that I’m handing them an interface. I just want them to know that it’s a ShapeFactory. So if I must encode either the interface or the implementation, I choose the implementation. Calling it shapeFactoryImp, or even the hideous CShapeFactory, is preferable to encoding the interface.
In general programmers are pretty smart people. Smart people sometimes like to show off their smarts by demonstrating their mental juggling abilities. After all, if you can reliably remember that r is the lower-cased version of the url with the host and scheme removed, then you must clearly be very smart.
One difference between a smart programmer and a professional programmer is that the professional understands that clarity is king. Professionals use their powers for good and write code that others can understand.
--Clean Code by Robert C. Martin
ABSTRACT FACTORY for the creation of shapes. This factory will be an interface and will be implemented by a concrete class. What should you name them? IShapeFactory and ShapeFactory? I prefer to leave interfaces unadorned. The preceding I, so common in today’s legacy wads, is a distraction at best and too much information at worst. I don’t want my users knowing that I’m handing them an interface. I just want them to know that it’s a ShapeFactory. So if I must encode either the interface or the implementation, I choose the implementation. Calling it shapeFactoryImp, or even the hideous CShapeFactory, is preferable to encoding the interface.
In general programmers are pretty smart people. Smart people sometimes like to show off their smarts by demonstrating their mental juggling abilities. After all, if you can reliably remember that r is the lower-cased version of the url with the host and scheme removed, then you must clearly be very smart.
One difference between a smart programmer and a professional programmer is that the professional understands that clarity is king. Professionals use their powers for good and write code that others can understand.
--Clean Code by Robert C. Martin
Wednesday, March 17, 2010
Atomically incremented counters
private final static int startIndex = 1;
private final static int pageSize = 5000;
private AtomicInteger pageNo = new AtomicInteger(startIndex);
private int getNextPage (){
return pageNo.getAndIncrement();
}
insteadOf**
private synchronized int getNextPage (){
pageNo++;
return pageNo;
}
#####################################
# More on it
#####################################
import java.util.concurrent.atomic.AtomicInteger;
public class AtomicIntegerUsage {
private final static int startIndex = 1;
private final static int pageSize = 5000;
private AtomicInteger pageNo = new AtomicInteger(startIndex);
private AtomicInteger anotherPageNo = new AtomicInteger(startIndex);
private int getNextPage_getAndIncrement (){
return pageNo.getAndIncrement();
}
private int getNextPage_incrementAndGet (){
return anotherPageNo.incrementAndGet();
}
public AtomicIntegerUsage () {
}
public static void main (String[] args) {
AtomicIntegerUsage t = new AtomicIntegerUsage ();
System.out.println (" First page is page with getNextPage_getAndIncrement :"+t.getNextPage_getAndIncrement());
System.out.println (" First page is page with getNextPage_incrementAndGet:"+t.getNextPage_incrementAndGet());
}
}
First page is page with getNextPage_getAndIncrement :1
First page is page with getNextPage_incrementAndGet:2
private final static int pageSize = 5000;
private AtomicInteger pageNo = new AtomicInteger(startIndex);
private int getNextPage (){
return pageNo.getAndIncrement();
}
insteadOf**
int pageNo = 0;
pageNo++;
return pageNo;
}
#####################################
# More on it
#####################################
import java.util.concurrent.atomic.AtomicInteger;
public class AtomicIntegerUsage {
private final static int startIndex = 1;
private final static int pageSize = 5000;
private AtomicInteger pageNo = new AtomicInteger(startIndex);
private AtomicInteger anotherPageNo = new AtomicInteger(startIndex);
private int getNextPage_getAndIncrement (){
return pageNo.getAndIncrement();
}
private int getNextPage_incrementAndGet (){
return anotherPageNo.incrementAndGet();
}
public AtomicIntegerUsage () {
}
public static void main (String[] args) {
AtomicIntegerUsage t = new AtomicIntegerUsage ();
System.out.println (" First page is page with getNextPage_getAndIncrement :"+t.getNextPage_getAndIncrement());
System.out.println (" First page is page with getNextPage_incrementAndGet:"+t.getNextPage_incrementAndGet());
}
}
First page is page with getNextPage_getAndIncrement :1
First page is page with getNextPage_incrementAndGet:2
Java Thread Local Usage
private ThreadLocal tl = new ThreadLocal(){
protected synchronized Session initialValue() {
return sessionForBatchLoading;
}
};
private Session sessionForBatchLoading = null;
private void initializeSession () throws Exception {
session = SessionFactory.getSession ();
sessionForBatchLoading = factory.getSession ();
tl.set(sessionForBatchLoading);
}
private void getOrder (int orderId) throws Exception {
Session tlSession = tl.get ();
tlSession.getItemFromDBWithId (orderId);
}
*Session is not thread safe .
private void startThreads (int threadCount) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
for (int i = 0; i < threadCount; i++) {
FetchTask task = new FetchTask ();
executor.submit (task);
}
executor.shutdown ();
}
Subscribe to:
Posts (Atom)