I’ve never worked with Concurrency library before.
public class QueueExecutor {
static final int defaultCorePoolSize = 5;
static final int defaultMaximumPoolSize = 10;
static final long defaultKeepAliveTime = 10;
static final TimeUnit defaultTimeUnit = TimeUnit.MINUTES;
static final BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>();
private static ThreadPoolExecutor instance;
private QueueExecutor() {
instance = new ThreadPoolExecutor(defaultCorePoolSize, defaultMaximumPoolSize, defaultKeepAliveTime, defaultTimeUnit, workQueue);
}
public static ThreadPoolExecutor getInstance() {
if (instance == null) {
QueueExecutor();
}
return instance;
}
public static add(Runnable runnable){
} instance.execute(runnable);
}
My question is the following, If this object is running inside JBoss application container, should I synchronize add and getInstance functions and why? I think that these ThreadPoolExecutor is already syncronized.
There are two things I can say.
Declare
instancefinal (and initialize it accordingly). Since it is static it will only be created on class initialization, and the only time you will invoke the class is togetInstance(). If you do that you don’t need to worry about synchronization.The reason
adddoes not need to be synchronized is because theexecutemethod handles all the synchronization for you.Those two points being said, it is suggested to avoid creating your own thread in a J2EE environment. You can read more here