Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread work balance in a Producer-Consumer implementation  RSS feed

 
Hải Đăng Nguyễn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a basic implementation of producer-consumer as follow:



My question is how to make number of threads: x ~ y to increase application performance and loading balance?
Anyone have a keyword or hint? Thanks you in advance!
Sorry for my bad English!
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to use ExecutorService.. They have several methods like newCacheThreadPool, newFixedThreadPool and others to control number of threads..
 
Stephan van Hulst
Saloon Keeper
Posts: 6980
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Tushar. Don't think of x and y as threads. Think of them as tasks. You can submit both the x and the y tasks to the same executor service.

Be careful though. If your executor service has less threads in it's pool than you have maximum concurrent x or y tasks, you run the risk of deadlock, because all threads can be blocked waiting for elements to be inserted or removed from the queue. A solution is to assign a priority to tasks, depending on how full the queue is. If the queue is very full, y tasks receive a higher priority, and vice versa.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!