As a part of my project i need to create a threadpool containing fixed no of threads.when ever the threads are allocated to different processes , i need to allocate that many sessions too along with the threads.I want to use a ConcurrentLinkedQueue(of fixed size) to store the sessions so that as and when the threads are done ,i can put back my sessions into the Queue ,making it available to other processes.Hope my requirement is been made clear...Can any one give me some quidelines as of how this can be implemented..?how a ConcurrentLinkedQueue can be used..?
2 Answers
when ever the threads are allocated to different processes ...
You won't be able to share resources between threads in different processes with a ConcurrentLinkedQueue. It will be only accessible from the threads of one process.
If this is not the problem you can use a thread pool:
Executors.newFixedThreadPool(int nThreads, ThreadFactory threadFactory)
A ThreadFactory can associate the Session resource with the threads managed by the thread pool using a ThreadLocal. You can configure different initialization strategies. Don't forget to clean up the sessions when the thread pool shuts down.