开发者

Thread number and Java application performance

Hi: I ha开发者_StackOverflow社区ve a multi thread Java application. The current thread size is already 100. We are currently using 4 core CPU. But as one see in the near future, CPU core would be doubled, or even to 32 cores. In order to fully utilize cores, we need to increase our thread pool size. But as you may know (Maybe I am wrong), Java is good when there is 100 hundred threads, but there could be performance problem when thread is 200, 500, 1000 threads. Then shall we use other programming language, for example scala. Is my worry reasonable?


With modern JVMs, a Java process can create as many threads as the operating system will permit. Whether or not your application will be able to make good use of those threads depends on the design of your application.

If scalability is a concern, I would recommend that in the first instance you focus on your application's architecture (data structures, synchronization, etc). These issues need to be considered irrespective of the programming language, and there's nothing about Java that makes it inherently unsuitable for heavily multithreaded apps.


I once made experiments with threads, to find out, whether there is significant difference between Linux and Windows, and hit a kind of barrier at about 2000 threads on both platforms. The test is some years old, and I didn't repeat it, but later I found the same number mentioned by others, but I didn't save the link.

Without testing it, I think you're right about scala. The techniques used there - Actors - works with smaller objects, afaik, but I can't give you numbers.


If you have 4 cores, the optimal thread pool size may be 4 as this is the minimum number of threads required to keep all the CPUs busy. However, you can have any number of idle/waiting threads up to about 10K. This is a JVM thread library tipping point so switching to Scala won't make any difference. Note: you can have far more threads, I wouldn't recommend it.

If you have 10K threads and you want more, I suggest you buy another server. You can buy a lot for server for about $1000.

I ran a test creating lots of threads on my machine with Java 6 update 26, 32-bit and 64-bit on Ubuntu 11. The first 1000 threads took 72 ms to create, to go from 31K to 32K, took 3,861 ms to create. At about 32K threads I got this error

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:640)


@user84592: not sure about my answer, just brainstorming. How about having installing virtual machine software on this machine, distributing CPU cores to them, it will make many machines instead of having one physical machine, and then you can have java application workload sliced to each of them...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜