ConcurrentLinkedQueue usage with multithreading
As a part of my project i need to create a threadpool containing fi开发者_如何学Pythonxed 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..?
I assume you want to do the same thing as
Executors.newFixedThreadPool(n);
It not clear why you don't just use this thread pool.
It appears also you want to use a Queue as an object pool. You can use add()
to and poll()
to see if a free element is available.
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.
精彩评论