views:

2447

answers:

8

Does this vary by vendor? by operating system? other factors?

+12  A: 

Um, lots.

There are several parameters here. The specific VM, plus there are usually run-time parameters on the VM as well. That's somewhat driven by the operating system: what support does the underlying OS have for threads and what limitations does it put on them? If the VM actually uses OS-level threads at all, the good old red thread/green thread thing.

What "support" means is another question. If you write a Java program that is just something like

   class DieLikeADog {
         public static void main(String[] argv){
             for(;;){
                new Thread(new SomeRunaable).start();
             }
         }
    }

(and don't complain about little syntax details, I'm on my first cup of coffee) then you should certainly expect to get hundreds or thousands of threads running. But creating a Thread is relatively expensive, and scheduler overhead can get intense; it's unclear that you could have those threads do anything useful.

Update

Okay, couldn't resist. Here's my little test program, with a couple embellishments:

public class DieLikeADog {
    private static Object s = new Object();
    private static int count = 0;
    public static void main(String[] argv){
        for(;;){
            new Thread(new Runnable(){
                    public void run(){
                        synchronized(s){
                            count += 1;
                            System.err.println("New thread #"+count);
                        }
                        for(;;){
                            try {
                                Thread.sleep(1000);
                            } catch (Exception e){
                                System.err.println(e);
                            }
                        }
                    }
                }).start();
        }
    }
}

On OS/X 10.5.6 on Intel, and Java 6 5 (see comments), here's what I got

New thread #2547
New thread #2548
New thread #2549
Can't create thread: 5
New thread #2550
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
        at java.lang.Thread.start0(Native Method)
        at java.lang.Thread.start(Thread.java:592)
        at DieLikeADog.main(DieLikeADog.java:6)
Charlie Martin
How much memory did you start the JVM with? That matters.
Eddie
the default. Questioner has a test program now, and I want another cup of coffee.
Charlie Martin
Okay, you tempted me, @Eddie, but messing about with -Xmx and -Xss just lead to a lot of combinations that don't work well. I need an undergrad who wants a research project.
Charlie Martin
I'm embarrassed to say that my laptop can only start a little over 900 Threads with your test program before it chokes.
Eddie
WinXP (32bit), C2Q6600, 4GB RAM, jdk1.6.0_11 (32bit) with default JVM options gets me #5587 threads. Strangely with more JVM memory I can create less threads: with the "-Xms1G -Xmx1G" JVM options only #2644.
Esko Luontola
But I can have multiple such JVMs with 5000 threads (see Windows Task Manager | Performance | Totals | Threads), so apparently the OS limit is much higher.
Esko Luontola
Yeah, the Windows JVM gets lots of attention, it's usually about the best. Oddly, even better than Solaris JVM on some things. The thing is the number of threads is going to be a function of all of -Xms -Xmx and -Xss, with the answer being an optimum among the three.
Charlie Martin
I'd expect that, a Windows thread is a per-process resource. Linux threads are lightweight processes managed by the general scheduler.
Charlie Martin
Actually, mine is with Java 5, not 6. I thought I had 6 on this thing....
Charlie Martin
... aha. For the good reason that Java 6 won't run on this elderly mac mini I use as a writing machine.
Charlie Martin
Java 6 update 13, Ubuntu 8.10 32 Bit, 4Gig ram, Default JVM settings = 6318 Threads.
Steve K
Heh, play with the thread stack size. java -Xss100k allowed me to create 19702 threads in Linux.
Steve K
java -Xss50k got me around 32k threads. That maxxed out my 4gigs of ram though. I had to stop some running processes to get enough memory back on my machine to fork a new process to kill java ;) - good times.
Steve K
See, that's the thing. -Xss is allocating space for the separate thread stacks; that's competing for space with the heap, and limited by -Xmx. So you really need to explore at least three dimensions, and probably more. As I say, this calls for an undergrad research project.
Charlie Martin
I'm getting almost the same results as you, Charlie Martin, on my Intel OSX 10.5.6 with both Java 5 and Java six with various combination of Xss, Xmx and Xms. There is something else in play here.
Stu Thompson
The OS/X implementation is pretty different from the others, different code base, which is why it took so long to get Java 6. We've certainly established the base point of "it depends". I'll ping some of my erstwhile colleagues at Sun and see what I can find out.
Charlie Martin
+5  A: 

This depends on the CPU you're using, on the OS, on what other processes are doing, on what Java release you're using, and other factors. I've seen a Windows server have > 6500 Threads before bringing the machine down. Most of the threads were not doing anything, of course. Once the machine hit around 6500 Threads (in Java), the whole machine started to have problems and become unstable.

My experience shows that Java (recent versions) can happily consume as many Threads as the computer itself can host without problems.

Of course, you have to have enough RAM and you have to have started Java with enough memory to do everything that the Threads are doing and to have a stack for each Thread. Any machine with a modern CPU (most recent couple generations of AMD or Intel) and with 1 - 2 Gig of memory (depending on OS) can easily support a JVM with thousands of Threads.

If you need a more specific answer than this, your best bet is to profile.

Eddie
A: 

I recall hearing a Clojure talk where he got to run one of his apps on some specialized machine at a trade show with thousands of cores (9000?), and it loaded them all. Unfortunately, I can't find the link right now (help?).

Based on that, I think it's safe to say that the hardware and your code are the limiting factors, not the JVM.

Ken
could you look again? I would like to see it - it sounds interesting and support that functional languages are easy to scale across cores.
Thorbjørn Ravn Andersen
Can you provide a link to that? I know that Cliff Click, Jr., the Lead Engineer from Azul Systems ran Rich Hickey's Ant Colony Simulation on Azul's largest JCA system (Azul Vega 3 Series 7300 Model 7380D: http://AzulSystems.Com/products/compute_appliance_specs.htm ) with 864 cores and 768 GB RAM, and the 700 ants managed to max out 700 cores. But 9000 cores, that's pretty impressive. What kind of machine was that?
Jörg W Mittag
A: 

If you are asking this question, you may want to rewrite your code.

HUAGHAGUAH
You *may*, but there's also some evidence that on recent operating systems, handling large numbers of connections with large numbers of threads is quite viable and the reasons why it didn't use to be are now becoming obsolete.
Neil Coffey
+3  A: 

After reading Charlie Martin's post, I was curious about whether the heap size makes any difference in the number of threads you can create, and I was totally dumbfounded by the result.

Using JDK 1.6.0_11 on Vista Home Premium SP1, I executed Charlie's test application with different heap sizes, between 2 MB and 1024 MB.

For example, to create a 2 MB heap, I'd invoke the JVM with the arguments -Xms2m -Xmx2m.

Here are my results:

2 mb --> 5744 threads
4 mb --> 5743 threads
8 mb --> 5735 threads
12 mb --> 5724 threads
16 mb --> 5712 threads
24 mb --> 5687 threads
32 mb --> 5662 threads
48 mb --> 5610 threads
64 mb --> 5561 threads
96 mb --> 5457 threads
128 mb --> 5357 threads
192 mb --> 5190 threads
256 mb --> 5014 threads
384 mb --> 4606 threads
512 mb --> 4202 threads
768 mb --> 3388 threads
1024 mb --> 2583 threads

So, yeah, the heap size definitely matters. But the relationship between heap size and maximum thread count is INVERSELY proportional.

Which is weird.

benjismith
would make sense if EACH thread is given a heap of that size.
Thorbjørn Ravn Andersen
Caveat: my machine does not have 2583 GB of RAM. Or swap. And the JVM doesn't allocate thread-local heap space. So that can't be it...
benjismith
The heap size reduces the address space available for stacks. Address space of 256K/stack makes sense.
Tom Hawtin - tackline
Ah yes, the stacks! Sounds about right.
benjismith
@Tom Hawtin - I just found this answer. Awesome stuff. Thanks for the education.
duffymo
A: 

After playing around with Charlie's DieLikeACode class, it looks like the Java thread stack size is a huge part of how many threads you can create.

-Xss set java thread stack size

For example

java -Xss100k DieLikeADog

But, Java has the Executor interface. I would use that, you will be able to submit thousands of Runnable tasks, and have the Executor process those tasks with a fixed number of threads.

Steve K
+1  A: 

The absolute theoretical maximum is generally a process's user address space divided by the thread stack size (though in reality, if all your memory is reserved for thread stacks, you won't have a working program...).

So under 32-bit Windows, for example, where each process has a user address space of 2GB, giving each thread a 128K stack size, you'd expect an absolute maximum of 16384 threads (=2*1024*1024 / 128). In practice, I find I can start up about 13,000 under XP.

Then, I think you're essentially into whether (a) you can manage juggling that many threads in your code and not do obviously silly things (such as making them all wait on the same object then calling notifyAll()...), and (b) whether the operating system can. In principle, the answer to (b) is "yes" if the answer to (a) is also "yes".

Incidentally, you can specify the stack size in the constructor of the Thread; you don't need to (and probably shouldn't) mess about with VM parameters for this.

Neil Coffey
So use a 64-bit OS. How long have we all been using 64-bit processors for now?
Tom Hawtin - tackline
Sure, I'm just giving an example of a theoretical vs practical limit. Mind you, there are an awful lot of 32-bit machines (including servers) still out there...
Neil Coffey
A: 

How many threads can I create?

I suppose that we can create number of threads depending on the CPU Cycles.

for example

class thread extends Thread

{

static int c=0;

public thread()

{

    super("my thread");

    System.out.println(this);

    start();

    c++;

    System.out.println(c);

}

    public void run()

    {

        try

        {

            for(int i=0;i<1;i++)

            System.out.println("thread");

        }

        catch(Exception e)

        {

            System.out.println("interrupted");

        }

    }

public static void main(String[] args) 

{

    for(int i=0;i<100000;i++)

    new thread();

}

}

Expected o/p:

Thread[my thread,5,main]

thread

1

thread

.

.

.

.

.

.

.

.

.

.

.

.

Thread[my thread,5,main]

thread

100000

thread

//that is we can create number of threads.

kalyani