1. 程式人生 > >「翻譯」一篇redis文章引發的翻譯——JVM能支持多少線程?

「翻譯」一篇redis文章引發的翻譯——JVM能支持多少線程?

easily over start cpu around hat for fan 計算

昨天看了一篇關於redis 的文章https://www.cnblogs.com/fanwencong/p/5782860.html

作者說他模擬了100萬線程的並發,我對這個有一些懷疑,看了評論也有很多質疑的聲音。當然我這篇不是要批評作者對線程的模擬,事實上作者寫的對redis的使用是很不錯的,我們本篇主要針對個人電腦上的JVM最多能支持多少個線程。以下是StackOverflow上的一個提問,我簡單的翻譯了一下。
StackOverflow原回答請點我


Eddie 的回答

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.

譯:這取決於你使用的CPU、操作系統、取決於其他進程在處理什麽事情、你使用的Java版本和其他因素。我曾經見過一個WINDOWS 服務器,它上面有超過6500個線程。當然大多數線程是空閑的。一旦電腦上達到6500個線程(在Java中),整個機器開始出現問題並且變得不穩定。我的經驗是無論計算機本身能夠維持多少線程,Java(最近版本的)都能吃得下,並且不出問題。當然你需要足夠的RAM並且你必須用足夠的內存來啟動Java來滿足這麽多線程的需要,並且需要有一個棧來存儲線程。任何使用現代CPU的計算機(最近兩代的AMD或Intel)和1-2G的內存(取決於操作系統)就可以支持JVM創建上千個線程。如果你需要比這個回答更具體的答案,最好的選擇是看一下相關的文檔。

Charlie Martin 的回答

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)

翻譯:

呃,很多。

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

有很多種情況。特定的虛擬機,特定VM,加上VM上通常還有運行時參數。跟操作系統也有點關系:運行的操作系統對多線程有怎樣的支持和對線程的有何種限制?如果VM完全使用操作系統級的線程,那即是紅線程/綠色線程的事情。(譯:這一句不太懂)

何種“支持”意味著另一種問題。如果你寫一個Java程序,就像這樣

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

(先不要抱怨語法上的細節,我在喝今天的第一杯咖啡),你應該期望創建成百上千的運行線程。但是創建一個線程是很昂貴的,調度器開銷也會變得很緊張。不清楚你用這些線程可以做什麽有用的事情。

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();
        }
    }
}

在 OS/X 10.5.6 on Intel, and Java 6 5 (請看評論)環境中, 我的運行結果如下:

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)

「翻譯」一篇redis文章引發的翻譯——JVM能支持多少線程?