0

Having to sink in my teeth into Java Executor Service I noticed that it falls flat for some of my requirements:

  1. I need to implement a small reactive system. So a Task can result in a new Task.
  2. The tasks schedule dynamically either as a reaction of an event (waiting for a token pool for instance) or by time.
  3. If a task dies I need to react accordingly so a task may be composed by different event methods or at least provide a listener service (like the Google guava lib)

I do not like the idea to implement it myself. But it is a event listener collection waiting for events to happen and immediately schedule tasks for this. Scheduling a job for a certain point in time is just also generating a timer event that is triggered by a waiting queue waiting for the nearest timer event to happen notify the framework and done.

So is there something around that provides this or a default way to use a Java Executor for this?

Martin Kersten
  • 5,127
  • 8
  • 46
  • 77

2 Answers2

0

Use the java concurrency package which borrows concepts from a very up and coming framework called akka. That should be enough to do what you want, if not then use akka. I have an example here: Executing Dependent tasks in parallel in Java

EDIT: Yes correct in my example there was blocking on the get methods, simply use the

future.onComplete(new OnComplete<Object>() )

To remove any blocking.

Community
  • 1
  • 1
Derrops
  • 7,651
  • 5
  • 30
  • 60
  • Your example over there is the exact reason why I can not use the demonstrated approach. First of all your example works only for this example due you restrict the executor pool and using FutureTask.get() blocks the current thread ["void java.util.concurrent.locks.LockSupport.park(Object blocker) -Disables the current thread for thread scheduling purposes unless the permit is available.]. This is undesirable. Also it is not really testable (the big nogo) and the reaction to internal task failures and a task rescheduling itself is not possible. – Martin Kersten Aug 21 '15 at 10:09
  • Hi, just use the onComplete instead, then there is no blocking. – Derrops Aug 21 '15 at 10:54
0

I found a final working solution for my self. Turned out that I just have to implement a special task and a simple executor of that task. The task can itself use any means to react and delay upon events. Waiting for a event is just a sequence of tasks or a event producer that creates tasks on behalf of an event. Simple as that.

But why building a scheduled executor using delays? That does makes no sense at all. No one ever wants to schedule tasks or events using a delay... .

Martin Kersten
  • 5,127
  • 8
  • 46
  • 77