ForkJoinPool cap on number of threads

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

ForkJoinPool cap on number of threads

Luke Sandberg
Hi,
I'm trying to use ForkJoinPool and I am concerned that the maximum number of threads it will create is capped at 32767.  Whereas I would like to set a much lower cap (4-16).

You can configure the FJP with a target parallelism but i don't know if it is possible to actually put a cap on the max parallelism.

In my specific case i will just be using it as an ExecutorService (basically execute() and shutdown()) with the 'asyncMode' bit set to 'true', and will _not_ be using the Fork/Join framework.  (I want to see if my application can get better performanced via decreased contention and better cache locality).  Based on my reading of the docs i think this means that it will not create any compensating threads since i would not be using the managed blocking facilities.

Does anyone have any experience with using this in production?  I am concerned that 'compensating' threads could cause my servers to run out of memory for thread stacks (at 1MB a piece they aren't cheap)' and my applications run in a shared hosting environment with strict caps on per process ram usage (If i go over my reservation, my server will be killed).  Currently, we manage this with ThreadPoolExecutor by exposing our maxSizes to our configuration and using that to set reservations.

Thanks,
Luke

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: ForkJoinPool cap on number of threads

Viktor Klang
Hi Luke,

the newest version of FJP has a tighter bound on max number of threads (search for maxSpares here: http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/main/java/util/concurrent/ForkJoinPool.java?view=markup)

Another way of limiting is to have the ThreadFactory return null when no more threads should be created.

On Tue, Mar 24, 2015 at 8:35 PM, Luke Sandberg <[hidden email]> wrote:
Hi,
I'm trying to use ForkJoinPool and I am concerned that the maximum number of threads it will create is capped at 32767.  Whereas I would like to set a much lower cap (4-16).

You can configure the FJP with a target parallelism but i don't know if it is possible to actually put a cap on the max parallelism.

In my specific case i will just be using it as an ExecutorService (basically execute() and shutdown()) with the 'asyncMode' bit set to 'true', and will _not_ be using the Fork/Join framework.  (I want to see if my application can get better performanced via decreased contention and better cache locality).  Based on my reading of the docs i think this means that it will not create any compensating threads since i would not be using the managed blocking facilities.

Does anyone have any experience with using this in production?  I am concerned that 'compensating' threads could cause my servers to run out of memory for thread stacks (at 1MB a piece they aren't cheap)' and my applications run in a shared hosting environment with strict caps on per process ram usage (If i go over my reservation, my server will be killed).  Currently, we manage this with ThreadPoolExecutor by exposing our maxSizes to our configuration and using that to set reservations.

Thanks,
Luke

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest




--
Cheers,

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: ForkJoinPool cap on number of threads

Luke Sandberg
Thanks!

Looks like i can have my ForkJoinThreadFactory return null to limit active threads. (it would be nice if that was documented on ForkJoinThreadFactory)

In that snapshot it looks like the maxSpares limitation only applies to the common pool, is there a plan to add a standard maxSize mechanism for user pools?

On Wed, Mar 25, 2015 at 2:06 AM, Viktor Klang <[hidden email]> wrote:
Hi Luke,

the newest version of FJP has a tighter bound on max number of threads (search for maxSpares here: http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/main/java/util/concurrent/ForkJoinPool.java?view=markup)

Another way of limiting is to have the ThreadFactory return null when no more threads should be created.

On Tue, Mar 24, 2015 at 8:35 PM, Luke Sandberg <[hidden email]> wrote:
Hi,
I'm trying to use ForkJoinPool and I am concerned that the maximum number of threads it will create is capped at 32767.  Whereas I would like to set a much lower cap (4-16).

You can configure the FJP with a target parallelism but i don't know if it is possible to actually put a cap on the max parallelism.

In my specific case i will just be using it as an ExecutorService (basically execute() and shutdown()) with the 'asyncMode' bit set to 'true', and will _not_ be using the Fork/Join framework.  (I want to see if my application can get better performanced via decreased contention and better cache locality).  Based on my reading of the docs i think this means that it will not create any compensating threads since i would not be using the managed blocking facilities.

Does anyone have any experience with using this in production?  I am concerned that 'compensating' threads could cause my servers to run out of memory for thread stacks (at 1MB a piece they aren't cheap)' and my applications run in a shared hosting environment with strict caps on per process ram usage (If i go over my reservation, my server will be killed).  Currently, we manage this with ThreadPoolExecutor by exposing our maxSizes to our configuration and using that to set reservations.

Thanks,
Luke

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest




--
Cheers,


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: ForkJoinPool cap on number of threads

Michael Spiegel-2
Maybe I am misunderstanding the question but a simple solution is just to use a semaphore with a fixed number of permits and use it to limit the number of tasks submitted.

On Wed, Mar 25, 2015 at 11:24 AM, Luke Sandberg <[hidden email]> wrote:
Thanks!

Looks like i can have my ForkJoinThreadFactory return null to limit active threads. (it would be nice if that was documented on ForkJoinThreadFactory)

In that snapshot it looks like the maxSpares limitation only applies to the common pool, is there a plan to add a standard maxSize mechanism for user pools?

On Wed, Mar 25, 2015 at 2:06 AM, Viktor Klang <[hidden email]> wrote:
Hi Luke,

the newest version of FJP has a tighter bound on max number of threads (search for maxSpares here: http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/main/java/util/concurrent/ForkJoinPool.java?view=markup)

Another way of limiting is to have the ThreadFactory return null when no more threads should be created.

On Tue, Mar 24, 2015 at 8:35 PM, Luke Sandberg <[hidden email]> wrote:
Hi,
I'm trying to use ForkJoinPool and I am concerned that the maximum number of threads it will create is capped at 32767.  Whereas I would like to set a much lower cap (4-16).

You can configure the FJP with a target parallelism but i don't know if it is possible to actually put a cap on the max parallelism.

In my specific case i will just be using it as an ExecutorService (basically execute() and shutdown()) with the 'asyncMode' bit set to 'true', and will _not_ be using the Fork/Join framework.  (I want to see if my application can get better performanced via decreased contention and better cache locality).  Based on my reading of the docs i think this means that it will not create any compensating threads since i would not be using the managed blocking facilities.

Does anyone have any experience with using this in production?  I am concerned that 'compensating' threads could cause my servers to run out of memory for thread stacks (at 1MB a piece they aren't cheap)' and my applications run in a shared hosting environment with strict caps on per process ram usage (If i go over my reservation, my server will be killed).  Currently, we manage this with ThreadPoolExecutor by exposing our maxSizes to our configuration and using that to set reservations.

Thanks,
Luke

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest




--
Cheers,


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest



_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: ForkJoinPool cap on number of threads

Martin Buchholz-3
In reply to this post by Luke Sandberg


On Wed, Mar 25, 2015 at 8:24 AM, Luke Sandberg <[hidden email]> wrote:
Thanks!

Looks like i can have my ForkJoinThreadFactory return null to limit active threads. (it would be nice if that was documented on ForkJoinThreadFactory)


I agree that we should make that clearer - there are existing snippets of javadoc, but we should add something (perhaps even sample code) showing how to impose a limit.
 
In that snapshot it looks like the maxSpares limitation only applies to the common pool, is there a plan to add a standard maxSize mechanism for user pools?

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: ForkJoinPool cap on number of threads

Martin Buchholz-3
TIL that ForkJoinPool.ForkJoinWorkerThreadFactory does not implement ThreadFactory!
Nevertheless, we can align the specs to allow the former to return null, like the latter.
This seems like small progress:

Index: src/main/java/util/concurrent/ForkJoinPool.java
===================================================================
RCS file: /export/home/jsr166/jsr166/jsr166/src/main/java/util/concurrent/ForkJoinPool.java,v
retrieving revision 1.240
diff -u -r1.240 ForkJoinPool.java
--- src/main/java/util/concurrent/ForkJoinPool.java 23 Feb 2015 20:54:08 -0000 1.240
+++ src/main/java/util/concurrent/ForkJoinPool.java 25 Mar 2015 19:03:36 -0000
@@ -681,7 +681,8 @@
          * Returns a new worker thread operating in the given pool.
          *
          * @param pool the pool this thread works in
-         * @return the new worker thread
+         * @return the new worker thread, or {@code null} if the request
+         *         to create a thread is rejected
          * @throws NullPointerException if the pool is null
          */
         public ForkJoinWorkerThread newThread(ForkJoinPool pool);


On Wed, Mar 25, 2015 at 11:23 AM, Martin Buchholz <[hidden email]> wrote:


On Wed, Mar 25, 2015 at 8:24 AM, Luke Sandberg <[hidden email]> wrote:
Thanks!

Looks like i can have my ForkJoinThreadFactory return null to limit active threads. (it would be nice if that was documented on ForkJoinThreadFactory)


I agree that we should make that clearer - there are existing snippets of javadoc, but we should add something (perhaps even sample code) showing how to impose a limit.
 
In that snapshot it looks like the maxSpares limitation only applies to the common pool, is there a plan to add a standard maxSize mechanism for user pools?


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: ForkJoinPool cap on number of threads

Luke Sandberg
I don't want to limit submisssions (requests queueing is fine, i also have various mechanism to limit blocking, my uses should be largely non-blocking), i need to limit the actual number of threads running.

Thanks martin!  I think that doc would be have been helpful (also the jdk 8 docs on FJP would have been useful, i am still on jdk7 and was looking at the older docs).

On Wed, Mar 25, 2015 at 12:07 PM, Martin Buchholz <[hidden email]> wrote:
TIL that ForkJoinPool.ForkJoinWorkerThreadFactory does not implement ThreadFactory!
Nevertheless, we can align the specs to allow the former to return null, like the latter.
This seems like small progress:

Index: src/main/java/util/concurrent/ForkJoinPool.java
===================================================================
RCS file: /export/home/jsr166/jsr166/jsr166/src/main/java/util/concurrent/ForkJoinPool.java,v
retrieving revision 1.240
diff -u -r1.240 ForkJoinPool.java
--- src/main/java/util/concurrent/ForkJoinPool.java 23 Feb 2015 20:54:08 -0000 1.240
+++ src/main/java/util/concurrent/ForkJoinPool.java 25 Mar 2015 19:03:36 -0000
@@ -681,7 +681,8 @@
          * Returns a new worker thread operating in the given pool.
          *
          * @param pool the pool this thread works in
-         * @return the new worker thread
+         * @return the new worker thread, or {@code null} if the request
+         *         to create a thread is rejected
          * @throws NullPointerException if the pool is null
          */
         public ForkJoinWorkerThread newThread(ForkJoinPool pool);


On Wed, Mar 25, 2015 at 11:23 AM, Martin Buchholz <[hidden email]> wrote:


On Wed, Mar 25, 2015 at 8:24 AM, Luke Sandberg <[hidden email]> wrote:
Thanks!

Looks like i can have my ForkJoinThreadFactory return null to limit active threads. (it would be nice if that was documented on ForkJoinThreadFactory)


I agree that we should make that clearer - there are existing snippets of javadoc, but we should add something (perhaps even sample code) showing how to impose a limit.
 
In that snapshot it looks like the maxSpares limitation only applies to the common pool, is there a plan to add a standard maxSize mechanism for user pools?



_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest