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


Groups > comp.lang.java.programmer > #31679 > unrolled thread

How to programatically limit the number of threads in a java

Started byAjay Paul <ajaykpaul@gmail.com>
First post2015-05-12 08:49 -0700
Last post2015-06-03 21:50 -0400
Articles 8 — 7 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  How to programatically limit the number of threads in a java Ajay Paul <ajaykpaul@gmail.com> - 2015-05-12 08:49 -0700
    Re: How to programatically limit the number of threads in a java Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-05-12 12:01 -0400
    Re: How to programatically limit the number of threads in a java Marcel Mueller <news.5.maazl@spamgourmet.org> - 2015-05-12 21:19 +0200
      Re: How to programatically limit the number of threads in a java Joerg Meier <joergmmeier@arcor.de> - 2015-05-13 09:48 +0200
        Re: How to programatically limit the number of threads in a java Arne Vajhøj <arne@vajhoej.dk> - 2015-06-03 21:51 -0400
          Re: How to programatically limit the number of threads in a java dolnotmail@nomail.com (e) - 2015-06-15 03:33 +0000
            Re: How to programatically limit the number of threads in a java Robert Klemme <shortcutter@googlemail.com> - 2015-06-15 08:23 +0200
      Re: How to programatically limit the number of threads in a java Arne Vajhøj <arne@vajhoej.dk> - 2015-06-03 21:50 -0400

#31679 — How to programatically limit the number of threads in a java

FromAjay Paul <ajaykpaul@gmail.com>
Date2015-05-12 08:49 -0700
SubjectHow to programatically limit the number of threads in a java
Message-ID<d2e186ee-1bab-4737-9462-9031b6e2839e@googlegroups.com>
How to programatically limit the number of threads in a java. I know the settings in different application servers such as websphere to do that, but how do we do it programatically.

[toc] | [next] | [standalone]


#31681

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2015-05-12 12:01 -0400
Message-ID<mit82h$8g6$1@dont-email.me>
In reply to#31679
On 5/12/2015 11:49 AM, Ajay Paul wrote:
> How to programatically limit the number of threads in a java. I know the settings in different application servers such as websphere to do that, but how do we do it programatically.

     By starting the number of threads you want, no more and no less.

     That's not a perfect answer, because it controls only the number
of threads *you* start (or allow to finish, or don't start at all).
If classes not under your direct control start their own threads, or
if the JVM itself starts threads for its own purposes, they are pretty
much immune to your influence except insofar as they offer a means to
provide hints and limits.

     Do you want to limit the total number of threads running in the
JVM?  Why?  What are you trying to accomplish?

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

[toc] | [prev] | [next] | [standalone]


#31685

FromMarcel Mueller <news.5.maazl@spamgourmet.org>
Date2015-05-12 21:19 +0200
Message-ID<mitjo5$dn$1@gwaiyur.mb-net.net>
In reply to#31679
On 12.05.15 17.49, Ajay Paul wrote:
> How to programatically limit the number of threads in a java.

Simply do not start more threads than wanted. :-)
Java will not start more than one thread automatically.

> I know the settings in different application servers such as websphere to do that,

Exactly. The number of threads need to be controlled by the application 
that starts them. Web application servers are this kind of application.

 > but how do we do it programatically.

Not at all.

Even if the JVM could control that what should be the action when the 
application requests another thread?
Throw a NoMoreThreadsException? This exception could not be reasonably 
handled by the application. I.e. it will cause unexpected aborts or 
other malfunction. The effect is comparable to out of memory exceptions, 
since some libraries rely on the availability of (a limited number of) 
threads, e.g. JMS or DBMS drivers. So basically you will need to restart 
the application server after that exception.
Wait until another thread terminates? This is called a thread pool. 
However, the application could deadlock, because the expected task may 
not complete in a reasonable time, e.g. especially when other threads 
wait for this one. Thread pools are commonly used by applications 
servers but they must handle only independent requests.


Marcel

[toc] | [prev] | [next] | [standalone]


#31691

FromJoerg Meier <joergmmeier@arcor.de>
Date2015-05-13 09:48 +0200
Message-ID<3yws2b8pk2kq.198es8aoaybyr.dlg@40tude.net>
In reply to#31685
On Tue, 12 May 2015 21:19:33 +0200, Marcel Mueller wrote:

Disclaimer: I only just now looked into this topic, so my answers may not
be the end all.

> On 12.05.15 17.49, Ajay Paul wrote:
>> How to programatically limit the number of threads in a java.
> Simply do not start more threads than wanted. :-)
> Java will not start more than one thread automatically.

Java might start (at least) three threads on its own (main, edt and garbage
collection).

>  > but how do we do it programatically.
> Not at all.

You can certainly prevent new threads being created programmatically.
Simply subclass SecurityManager, configure it to override checkAccess, and
implement restrictions on how new Thread objects are allowed to be created
in whatever fashion you deem apropriate. And don't forget to actually
install the SecurityManager.

I have demonstrated described solution here: http://ideone.com/edhsyF

> Even if the JVM could control that what should be the action when the 
> application requests another thread?
> Throw a NoMoreThreadsException? This exception could not be reasonably 

AccessControlException.

If the goal is to prevent untrusted code from creating new threads, this
may be a valid approach, otherwise, it will probably just make stuff break.

Alternatively, this could also be prohibited by setting the
SecurityConstants.MODIFY_THREADGROUP_PERMISSION and
SecurityConstants.MODIFY_THREAD_PERMISSION permissions to false, but
changing permissions is less easy to demonstrate.

Now, all of this took me about 20 minutes to research, because I thought it
was an interesting problem. I don't mean to be rude, but if you don't know
the answer to a question, instead of making one up, consider simply not
responding with a statement that looks like a fact.

Liebe Gruesse,
		Joerg

-- 
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.

[toc] | [prev] | [next] | [standalone]


#31763

FromArne Vajhøj <arne@vajhoej.dk>
Date2015-06-03 21:51 -0400
Message-ID<556faf0a$0$292$14726298@news.sunsite.dk>
In reply to#31691
[sorry if duplicate post]

On 5/13/2015 3:48 AM, Joerg Meier wrote:
 > On Tue, 12 May 2015 21:19:33 +0200, Marcel Mueller wrote:
 >
 > Disclaimer: I only just now looked into this topic, so my answers may not
 > be the end all.
 >
 >> On 12.05.15 17.49, Ajay Paul wrote:
 >>> How to programatically limit the number of threads in a java.
 >> Simply do not start more threads than wanted.
 >> Java will not start more than one thread automatically.
 >
 > Java might start (at least) three threads on its own (main, edt and 
garbage
 > collection).

I think that even the number of GC threads can be manipulated via
XX options (like -XX:ParallelGCThreads=n).

Arne

[toc] | [prev] | [next] | [standalone]


#31796

Fromdolnotmail@nomail.com (e)
Date2015-06-15 03:33 +0000
Message-ID<mllgve$6g9$1@dont-email.me>
In reply to#31763
In article <556faf0a$0$292$14726298@news.sunsite.dk>, =?windows-1252?Q?Arne_Vajh=F8j?= <arne@vajhoej.dk> wrote:
>[sorry if duplicate post]
>
>On 5/13/2015 3:48 AM, Joerg Meier wrote:
> > On Tue, 12 May 2015 21:19:33 +0200, Marcel Mueller wrote:
> >
> > Disclaimer: I only just now looked into this topic, so my answers may not
> > be the end all.
> >
> >> On 12.05.15 17.49, Ajay Paul wrote:
> >>> How to programatically limit the number of threads in a java.
> >> Simply do not start more threads than wanted.
> >> Java will not start more than one thread automatically.
> >
> > Java might start (at least) three threads on its own (main, edt and 
>garbage
> > collection).
>
>I think that even the number of GC threads can be manipulated via
>XX options (like -XX:ParallelGCThreads=n).
>
>Arne
>
>

A ThreadPoolExecutor is a good thing to use for this.

Eric

[toc] | [prev] | [next] | [standalone]


#31797

FromRobert Klemme <shortcutter@googlemail.com>
Date2015-06-15 08:23 +0200
Message-ID<cu79beFamlhU1@mid.individual.net>
In reply to#31796
On 15.06.2015 05:33, e wrote:
> In article <556faf0a$0$292$14726298@news.sunsite.dk>, =?windows-1252?Q?Arne_Vajh=F8j?= <arne@vajhoej.dk> wrote:

>> I think that even the number of GC threads can be manipulated via
>> XX options (like -XX:ParallelGCThreads=n).

> A ThreadPoolExecutor is a good thing to use for this.

No.  GC threads are controlled differently as Arne has shown.

Cheers

	robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [next] | [standalone]


#31764

FromArne Vajhøj <arne@vajhoej.dk>
Date2015-06-03 21:50 -0400
Message-ID<556faece$0$292$14726298@news.sunsite.dk>
In reply to#31685
[sorry if duplicate post]

On 5/12/2015 3:19 PM, Marcel Mueller wrote:
 > On 12.05.15 17.49, Ajay Paul wrote:
 >> How to programatically limit the number of threads in a java.
 >
 > Simply do not start more threads than wanted.
 > Java will not start more than one thread automatically.

I don't think that is correct.

The JVM do start threads besides the main thread.

What output do you get for:

public class ThreadCount {
     public static void main(String[] args) {
         System.out.println(Thread.activeCount());
 
System.out.println(Thread.currentThread().getThreadGroup().enumerate(new 
Thread[1000]));
 
System.out.println(Thread.currentThread().getThreadGroup().getParent().enumerate(new 
Thread[1000]));
     }
}

 > Even if the JVM could control that what should be the action when the
 > application requests another thread?
 > Throw a NoMoreThreadsException? This exception could not be reasonably
 > handled by the application. I.e. it will cause unexpected aborts or
 > other malfunction. The effect is comparable to out of memory exceptions,
 > since some libraries rely on the availability of (a limited number of)
 > threads, e.g. JMS or DBMS drivers. So basically you will need to restart
 > the application server after that exception.

True.

But we live with out of memory so if someone wanted to limit threads
then may be able to live with NoMoreThreadsException.

Arne

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web