Harshana Dias wrote:Now with in 39 mints, if user refresh the page, another thread initiate and do the same process. Mean time, the first thead read the status from redis cache as complete and remove it from cache. Now when second thread wake and check for course status in redis cache, it will never find it. So i want to finish that thread execution
Harshana Dias wrote:Where can i edit my question or reply?
Henry Wong wrote: Surely, this is easier that having the first thread hang around for awhile, looking for an undermined number of threads that may be started by accident, and communicating with them (or the executor) in order to abort those threads.
Henry
Stephan van Hulst wrote:Don't work with threads. Instead, when you create your tasks, assign them an object through which they can signal that they're finished. All other tasks that are part of the same group periodically check the same object if any of them has finished yet. Then they can stop themselves.
Paul Clapham wrote:You didn't like this answer? Why not?
Paul Clapham wrote:I don't see what that has to do with Stephan's suggestion. After all we already know that your tasks are going to run in separate threads, so what's the objection to having those tasks/threads communicate via a shared object?
Paul Clapham wrote:
So when one of those FutureTask objects completes, presumably it knows the courseId so it looks in that Map and finds all of the FutureTask<String> objects which are processing that courseId, and then stops them all.
Stephan van Hulst wrote:No. Like I suggested earlier, tasks should stop themselves. They need shared state to determine when to shut down. Here's how it roughly looks:
You need to keep a Map<String, Task> somewhere, and whenever you load the page, you just submit a new callable to your executor service:
Harshana Dias wrote:what ever tasks (threads)
Stephan van Hulst wrote: That means that the same Task instance will be shared between multiple threads, and that's why we need to use a Lock object to synchronize between them, and use signalAll() to wake them all up when the task is finished.
Harshana Dias wrote:Correction:
1.How come it print 457 with thread id 11 as there is a already task for course id 123? I expect 123 456 11.
Do you pee on your compost? Does this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|