Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #34593

Re: future run unlimited

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: future run unlimited
Date 2016-06-10 11:48 -0400
Organization A noiseless patient Spider
Message-ID <njengr$g9d$1@dont-email.me> (permalink)
References <2b896a25-e53e-4398-b423-fca4d040c3f9@googlegroups.com> <njei6e$si6$1@dont-email.me> <23604b1c-2b12-4af6-af2a-64efde2753a6@googlegroups.com>

Show all headers | View raw


On 6/10/2016 10:34 AM, Philipp Kraus wrote:
> Hi,
>
> thanks your answer, but I think I need to describe the problem a little bit better.
>
> Am Freitag, 10. Juni 2016 16:18:00 UTC+2 schrieb Eric Sosman:
>
>> If you want to call put() after each Callable completes, this won't
>> do it.  The filter() stage will not wait for completion; it will just
>> test "Has this Callable already completed?" and reject those that are
>> still in progress or have not started.  Result: The few (probably)
>> Callables that complete in a very small amount of time will pass to
>> the forEach() stage, and the stream will finish while the others are
>> still pending or in progress.
>>
>>      I'm not sure exactly what you want to do, but perhaps you should
>> take a look at ExecutorCompletionService.
>
> I would like to create a "continuous worker", something like
>
> while (true)
>      pool.invokeAll( mycallablelist )
>
> Like a queue, the pool gets a callable object from my queue / list and run the object, if it is done, it will put at the end of the list. I would like to replace the invokeAll with submit. I would like to create an asynchronized infinity working process.

     Let me restate what I think you want, in case I still misunderstand:
You have a fixed number of tasks, and each task should run over and over
again until you shut the whole thing down.  (If that's not right, then
disregard the rest ...)  Presumably, each task completion produces a
result or an effect that the overall program needs (otherwise, you'd
just write an infinite loop).  So: A task gets submitted once,
eventually produces its result, the result is consumed, and then
the task is re-submitted for another execution.

     If that's the goal, I think ExecutorCompletionService could help.
You would submit all N tasks to the ECS (a one-time "batch" operation),
take() each task as it finishes, consume its result, and re-submit.


>>      - Discard the tasks that have not started, wait for those already
>>        in progress, then shut down?
>
> in my case I can break every time and lost the non-started objects, but the running objects should be done / finished

     I don't know of a built-in way to do this.  Assuming the Executor is
actually an ExecutorService, it has a shutdown() method that will reject
further submissions but allow all submitted tasks to finish.  It also
has a shutdownNow() method that tries to cancel everything abruptly.
You seem to want something in between: Allow already-started tasks to
finish, while discarding those submitted but not yet started.

     Perhaps you could use shutdown() after setting a global "shutting
down now" flag.  Each task would check the flag before starting its
work, terminating immediately if it's set.  That way, all the tasks
would "run" but those that hadn't already started would finish "very
quickly" and drain out of the system.  (Of course, you won't re-submit
tasks while "shutting down now" is true -- if you tried, I think you'd
get RejectedExecutionException or something similar).

     Just curious: What is the nature of these over-and-over-again
tasks?  Can you describe the overall system?

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

future run unlimited Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2016-06-10 06:30 -0700
  Re: future run unlimited Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-10 10:17 -0400
    Re: future run unlimited Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2016-06-10 07:34 -0700
      Re: future run unlimited Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-10 11:48 -0400
        Re: future run unlimited Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2016-06-10 12:49 -0700
          Re: future run unlimited Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-11 08:22 -0400

csiph-web