質問

I am Sorry if my Question is Silly.

I am using ArrayBlockingQueue.

private ArrayBlockingQueue<RequestParameters> processorQueue;
private int sizeOfQueue = 3000;

Here, my Producer is seperate thread and Consumer is seperate Thread. both are running in Single Producer-Consumer Thread model.

Now, I am running my Java App, which gives request to my Servlet. Producer put the request in Queue. Consumer pics the request and start processing, which involves DB operations.(takes around 1 second to complete task)

Here, My Producer is getting request very fast and it fills Queue and wait until consumer start processing and make space in Queue.

My Question here is

  1. What best BlockingQueue Impl I should use, So that I will get best Performance.
  2. What will happen when Queue Size is Full, I mean Servlet will get request irrespective of Producer is waiting for Queue to get Vaccant. What about all those incoming Request? Where those request will go? are those request be stored in JVM Memory and given to Producer Thread once it is non-blocking?

Thanks.

役に立ちましたか?

解決

Though right solution will depend upon the fesibility of your application requirement, following might be helpful:

When Q is full instead of blocking indefinitely, block with timeout so as servlet will return response in a fix configured time. In case of timeout, servlet can throw server-busy kind of custom error message or related HTTP-CODE.

Since your consumer is slow and producer is very fast, you might as well consider increasing consumer threads so as to allow lesser blocking experience to servlet caller.

Also, accepting all request (ie storing into a unbounded-Q) wouldn't be a nice solution as (depending upon request size) due to slow consumer Q, over period of time might consume huge memory..

他のヒント

1) Don't use concrete implementations as types of fields or variables of return types of methods and etc. Use interfaces or abstract classes instead. In your case such class will be just BlockingQueue:

private BlockingQueue<RequestParameters> processorQueue = ...

2) You do not need to have additional field sizeOfQueue - you will pass its value into constructor of processorQueue.

3) Finally, you can use LinkedBlockingQueue implementation for such purposes:

private BlockingQueue<RequestParameters> processorQueue = new LinkedBlockingQueue(300);

What best BlockingQueue Impl I should use, So that I will get best Performance.

I'd be astonished if it made any difference given that you have a network in the system, but you should look to use one based on a linked list if FIFO performance is your criterion and there is no random access.

What will happen when Queue Size is Full

You specified BlockingQueue, so it will block.

This is all in the Javadoc.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top