Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Concurrent ExecutorService with partial sequential processing  RSS feed

 
Paweł Baczyński
Bartender
Posts: 1997
42
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a ready implementation (JDK, Guava, Apache Commons?) of an ExecutorService that would allow processing of submitted jobs concurrently,
but based on a certain condition (e.g. a job that would change the same user) it would execute those (user) jobs sequentially to each other (it would be still allowed to execute jobs regarding other users concurrently).

 
Stephan van Hulst
Saloon Keeper
Posts: 6980
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need. You can easily do this by yourself by submitting one task per user, with the task itself performing sub-tasks sequentially.
 
Paweł Baczyński
Bartender
Posts: 1997
42
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The point is, at the moment of submitting a task I don't have any information whether there are or will be more tasks that would touch the same user.
 
Stephan van Hulst
Saloon Keeper
Posts: 6980
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't test it, but it might be what you need:
 
Stephan van Hulst
Saloon Keeper
Posts: 6980
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An update with test:





Output:
Completed: Task B,0
Completed: Task C,0
Completed: Task A,0
Completed: Task B,1
Completed: Task C,1
Completed: Task A,1
Completed: Task B,2
Completed: Task C,2
Completed: Task A,2
Task A,3 failed with reason: java.lang.Exception: Whatever!
Task A,4 failed with reason: java.lang.Exception: Whatever!
Task A,5 failed with reason: java.lang.Exception: Whatever!
Task A,6 failed with reason: java.lang.Exception: Whatever!
Task A,7 failed with reason: java.lang.Exception: Whatever!
Task A,8 failed with reason: java.lang.Exception: Whatever!
Task A,9 failed with reason: java.lang.Exception: Whatever!
Completed: Task C,3
Completed: Task B,3
Completed: Task B,4
Completed: Task C,4
Completed: Task C,5
Completed: Task B,5
Task B,6 was cancelled.
Task B,7 failed with reason: java.util.concurrent.CancellationException
Task B,8 failed with reason: java.util.concurrent.CancellationException
Task B,9 failed with reason: java.util.concurrent.CancellationException
Completed: Task C,6
Completed: Task C,7
Completed: Task C,8
Completed: Task C,9
 
Paweł Baczyński
Bartender
Posts: 1997
42
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tested it briefly and it seems to works as expected! Thanks!
Too bad I am bound to use JDK 1.7.
But now I get the idea how this can be implemented. Thanks again. Have a cow.
 
Stephan van Hulst
Saloon Keeper
Posts: 6980
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paweł.

I've tried a few things, but I can't really think of an equally satisfying solution in Java 7. I'll ponder on it a little bit more.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!