0

In ExecutorService, how can I group all the threads processing a specific course id and then upon one thread finish process that course id, close all the other threads which are in the process of that course id?

The number of threads which can process the course id untill one thread finish process, is not a fixed number.

Update:

So any threads enter to process this course, I am going to add them to a static collection.

Suppose for courseid1, thread1 is initially processing,

List l1 = new ArrayList();
l1.add(thread1);

Map t = new Hashmap();
t.add("courseid1":l1)

So while thread1 is in process another thread comes to process courseid1,

l1.add(thread2);

Then once thread1 see the course status is complete, I will send success to all the threads in list l1.

Harshana
  • 7,297
  • 25
  • 99
  • 173
  • The future you get from the executor has `cancel` method which might be able to do that. Of course you'd have to maintain some sort of task registry so that you know what to cancel. – mszymborski Jul 24 '16 at 17:01

1 Answers1

2

I would use the parallelStream This will stop all the tasks once one fails, or if you specify you want the first result.

Optional<Result> res = tasks.parallelStream().map(t -> process(t)).findFirst();
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Thanks. I am new to this parallelStream in java 8. Any way, in you code sample what is the type of the tasks variable and where is the mapping of, ok these are the all threads which are currently processing this course? If you can explain further your sample code line, I would appreciate a lot. – Harshana Jul 24 '16 at 17:10
  • @Harshana The `tasks` are a list of all the actions you want the threads to perform and `process` is the method you want to call to make that processing happen for that task. – Peter Lawrey Jul 24 '16 at 17:12
  • Worth to note `process` should be a pure function (with no side effect). Also, how is the number of threads going to be configurable with this solution ? – Dici Jul 24 '16 at 17:16
  • 1
    @Dici You can use a custom ForkJoinPool http://stackoverflow.com/a/22269778/2770572 – michaelsnowden Jul 24 '16 at 17:24
  • @PeterLawrey I have one task to do in my situation. A thread listen to a course untill its completed and once complete send the response as success. How I listen is, I recursively sleep and wake the thread for a given time interval. So while this thread listen, multiple other threads can also do the same and all of those threads should finish listen and return success reposnse if any of thread sees the course status is complete. In that context, with my update question, can you elaborate more please? – Harshana Jul 24 '16 at 17:44
  • 1
    @Harshana each thread has a task to do even if they are all the same. I am not clear as to why you have multiple threads doing the same thing if many/most of them are asleep. – Peter Lawrey Jul 25 '16 at 08:41
  • @Peter Lawrey Its a long poll request. Firstly a thread would listen to a course status in a redis cache. If its not complete, go back to sleep for 10 seconds and wake and check again untill for 30 mits. If status is not complete with in that time, it will send error. 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 Jul 25 '16 at 09:18
  • @Dici process should be a pure function means? – Harshana Jul 29 '16 at 18:10
  • 2
    @Harshana without side-effect. In functional style functions should not modify any external or internal state when they run, so that they have the same result for the same input no matter when you run them, which thread runs them, or even which machine runs them (distributed computing) – Dici Jul 30 '16 at 12:07