简体   繁体   中英

ConcurrentLinkedQueue usage with multithreading

作为我项目的一部分,我需要创建一个包含固定线程数的线程池。每当线程分配给不同的进程时,我也需要与线程一起分配那么多会话。我想使用ConcurrentLinkedQueue(大小)以存储会话,以便在线程完成后将会话放回Queue中,以供其他进程使用。希望我的要求已明确...任何人都可以给我一些魁德从如何实现开始..?如何使用ConcurrentLinkedQueue ..?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM