Ok this could be a very big question.
I have one service(say notifier) and one of method [say notifyTransaction(Transaction trans) ] of this service is remotely available for callbacks.
Now there is other service say (transactionsReader) which keeps reading incoming transactions and calls up the remote method of notifier that is notifyTransaction(Transaction trans).
Now to explain you the problem, I would like to add that my transaction contains more than one Operations. And for treating operations i have one function say processOperation(Operation op).
This processOperation(Operation op) actually do some updates on a database.
Whats the problem ?
The service transactionsReader is reading incoming transactions at very fast speed and calling up the notifyTransactions() at the speed much greater than the speed of processing operations by processOperation(Operation op) method.
What I want to do ?
I want to use multithreadint in notifyTransactions() so that I have multiple threads to treat every transaction.
For starting I used
ExecutorService executor = Executors.newFixedThreadPool(2);
in the method notifyTransactions() and created task of my method processOperation(Operation op). But this creates a different pool for every call to notifyTransactions() so I had somewhere around 3000 pools in few seconds. And finally it went Out of Memory.
What could be other possible solutions ?
Thanks in advance 🙂
This is the issue you need
ExecutorServiceinstance to be common in both. If you start invoking per operation then it will create large number of threads. You should just callsubmittask insidenotifyTransactions()You should use a producer consumer approach using BlockingQueue which is bounded. So if queue is full producer will stop if queue is empty consumers will stop.
So in your case
processOperation(Operation op)is a producer which will process operation and put it in BlockingQueue and insidenotifyTransactions()you will dequeue it and process accordingly.