I have a module which operates on thousands of transactions to process . Each transaction has multiple phases to go through. This module executes in multithreaded mode. We have defined limits (hardcoded)for number of threads it can create (limited as per server utilization ).
now we came accross a situation, where threads may need to wait for some period (may be more than 24 hours). As we have limited number of threads and if all threads are waiting for more than 24 hours, this is entirely blocking the application.
What i need here is, how should i reuse the thread which is under wait wait for 24 hour. if thread is going in wait mode, i need to reuse that thread for anather transaction and when original wait ends, restart the original transaction from where it was put on hold.
i hope above discription helps u to understand issue.
If you have long delays in your system, the best thing to do is to have more threads. If you want to limit the number of threads running concurrently you can use permits e.g. a Semaphore which is released whenever you have blocking operation and re-acquired when the blocking finishes. This ensures you have a limited number of threads running at once but allows you to easily switch between many tasks.
In this example, you can have as many of these as you want but only a limited number will be within the PERMIT area. The tasks can also call runBlockingTask() to allow it to perform a blocking task but allow another thread to run while it is blocking.