7

How to set limit to the number of Thread that someone can create? What I do is running someone's code (something like ideone), and want to limit number of thread that he can spawn. How to do so? Some jvm setting or something else?

EDIT I add more specified info because some people are not gettin my point.

  1. Some random guy send me a code which my computer is going to execute
  2. Code must be execute within using maximum of k threads
  3. All must be automated - working like SPOJ, ideone, etc.
Raedwald
  • 46,613
  • 43
  • 151
  • 237
abc
  • 2,371
  • 3
  • 25
  • 36
  • java byte-code injecter maybe? Like a java-profiler but adds things instead of examining. You can limit the jvm-memory space before launching but its not exact value there can be 1000 or 10000, thread size important too. Also limiting memory space may result opposite effect. – huseyin tugrul buyukisik Jun 27 '13 at 11:30
  • Use a ThreadPool, with a limit – Bob Flannigon Jun 27 '13 at 11:37
  • @BobFlannigon It's not my code. My machine is going only to run it and what I want is to forbid starting new thread. For instance it can even crash if someone spawn Thread but in any case it can't execute. How to do so? – abc Jun 27 '13 at 11:40

3 Answers3

6

On Linux, you could run the program as a separate user and use the shell command ulimit -u nprocs to limit the number of threads (processes) for that user. If an attempt is made to exceed the limit, the JVM will throw an OutOfMemoryError.

But why do you want to do this? Are you worried that the program will consume all the CPU resources of the computer? If so, you might want to consider running the JVM at lower scheduling priority, using nice, so other processes will get preferential use of the CPU:

 NPROCS=100 # for example
 NICENESS=13 # for example
 ulimit -u $NPROCS
 nice -n $NICENESS java ...

Using nice in that manner should reduce the priority of all the threads, but it is not clear that it does so for Linux.

Community
  • 1
  • 1
Raedwald
  • 46,613
  • 43
  • 151
  • 237
  • In fact I will try to write SPOJ like system it quite easy to find out in C/C++ when someone attemp to create thread or demon using pthread (fork and so on can be track down). I thought java Thread has nothing to with physical thread. That's why asking about some jvm setting. Up voted. About cpu resource I will use cpulimit. You can set time which process can use in % – abc Jun 27 '13 at 13:38
2

You can create your own subclass for thread that performs the desired checking in the constructor(s) or in the start method.

To ensure the code you are running uses your custom thread class, you must load the code with your own custom class loader and that class loader simply catches any request for the java.lang.Thread class and hands out your custom class instead (the concept can be extended to other classes as well).

Warning: Implementing this properly is not trivial.

Durandal
  • 19,919
  • 4
  • 36
  • 70
  • It's nice idea to problem I present so up voted too, but wouldn't use it it's more language specific, which in fact is my fault - my question was java specific :) – abc Jun 27 '13 at 13:43
0

AFAIK,Limit is purely depends on OS not on JVM.

And you can Monitor them by a Executor service

An Executor that provides methods to manage termination and methods that can produce a Future for tracking progress of one or more asynchronous tasks.

ExecutorService pool = Executors.newFixedThreadPool(n);
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • How ExecutorService helps me if it's not my code and some could use extends Thread? Limited are only threads from this pool but it not prevents somebody from making other pool or just simply extends Thread. – abc Jun 27 '13 at 11:41