> --
> You received this message because you are subscribed to the Google Groups "Clojure Dev" group.
> To post to this group, send email to cloju...@googlegroups.com.
> To unsubscribe from this group, send email to clojure-dev...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/clojure-dev?hl=en.
>
>
--
And what is good, Phaedrus,
And what is not good—
Need we ask anyone to tell us these things?
> doesn't that just move the issue around? instead of hanging on the
> non-deamon threads you hang on the shutdown hook?
I believe the shutdown hook would finish once all the agents and futures
finished their execution. IIRC the agent threadpool doesn't allow an
exit until 60 seconds or so after its last use.
-Phil
Adding a shutdown hook will have no effect on the above behavior as
shutdown hooks are only run when a JVM is terminated, and non-daemon
threads prevent a JVM from terminating.
Personally I favor making these pools use daemon threads. The
contributed the patch (applied in 1.3) that uses thread pools with
custom names in 1.3 and it would be trivial in that thread factory to
call thread.setDaemon(true) around Agent line 61. Some people have
objected to this as one pattern is to have the main thread kick off a
bunch of work, then exit allowing the work to complete. There are
many simple ways to fix this problem (awaiting on the agent, starting
a keep-alive thread, etc). I discussed this in more depth here:
http://tech.puredanger.com/2010/06/08/clojure-agent-thread-pools/
Alex
In general, shutdown hooks can be tricky and are to be avoided if
possible. They create strange garbage collection scenarios, weird
problems with finalization (which runs after shutdown hooks), and
problems with other shutdown hooks (as they all run concurrently). I
believe the introduction of shutdown hooks are also restricted under
some security policies.
Is there a strong reason for using one? If you wish to wait for the
threads to complete, your program may be able to do that via awaiting
on the agent (depending on who called what). You can also call
Agent.shutdown() (not sure if there is a Clojure wrapper for this)
which will wait for pending jobs to finish. Or if you want to kill
things more urgently, you can call Agent.soloExecutor.shutdownNow()
and Agent.pooledExecutor.shutdownNow().
In other words, there are hooks available to solve these problems now,
and introducing shutdown hooks may introduce new and even stranger
problems, so it seems not worth the added potential complexity to me.
On Wed, Jul 6, 2011 at 4:22 PM, ataggart <alexcloj...@gmail.com> wrote:
> On Jul 6, 12:56 pm, Alex Miller <a...@puredanger.com> wrote:
>> The 60 second delay is due to the configuration of the thread pools
>> used by the agents.
>
> Ah, I see now, it's the behaviour as specified by
> Executors.newCachedThreadPool(), and not any clojure-specific
> configuration.
>
>> Adding a shutdown hook will have no effect on the above behavior as
>> shutdown hooks are only run when a JVM is terminated, and non-daemon
>> threads prevent a JVM from terminating.
>
> True, which is why I also noted the pools should use daemon threads.
> Personally I favor making these pools use daemon threads. The
> contributed the patch (applied in 1.3) that uses thread pools with
> custom names in 1.3 and it would be trivial in that thread factory to
> call thread.setDaemon(true) around Agent line 61.
Strongly seconded; we have had a few subtle errors creep into Leiningen
plugins due to having to work around the shortcomings of the current
thread pool behaviour.
-Phil