简体   繁体   English

可以为双核处理器创建多少个Java线程

[英]How many java threads can be created for a dual core processor

So the STB is dual core. 所以机顶盒是双核心。 I thought we can only create 2 proper threads. 我以为我们只能创建2个合适的线程。

In every keyreleased() 在每个keyreleased()中

I am creating a new thread 我正在创建一个新线程

Runnable runnable = new Runnable() 
{
    int i = j;

    public void run() 
    {
        while (true) 
        {
            System.out.println("This thread is running always number is " + i);
        }
    }
};

Thread th = new Thread(runnable);
            th.setPriority(Thread.MAX_PRIORITY);
            th.start();

j++;
//...
}

But even after creating 20 more threads, box doesn't have any issues. 但即使在创建了20多个线程之后,盒子也没有任何问题。

Is it because JVM realized that the run block is empty and it optimized the code ? 是因为JVM意识到运行块是空的并且它优化了代码? Or is the JVM implementation for while(true) is different ? 或者while(true)的JVM实现是不同的?

Note: i have tried putting Thread.sleep(1000) as well but no problems 注意:我也尝试过使用Thread.sleep(1000),但没有问题

You can run 20 threads even on a single-core machine. 即使在单核机器上也可以运行20个线程。 What happens is called time slicing. 会发生什么叫做时间切片。

http://en.wikipedia.org/wiki/Time_slice#Time_slice http://en.wikipedia.org/wiki/Time_slice#Time_slice

It is a way for the processor to simulate multiple processors executing multiple tasks as once. 这是处理器模拟多个处理器执行多个任务一次的方法。

The number of possible threads has nothing to do with CPU cores. 可能的线程数与CPU内核无关。 It is rather a function of available memory. 它相当于可用内存的功能。 Every thread needs a separate stack, so depending on stack size, the number is limited. 每个线程都需要一个单独的堆栈,因此根据堆栈大小,数量是有限的。

For example, try: 例如,尝试:

java -Xss8m -Xmx64m .....

You'll probably notice that you can't create that many Threads. 您可能会注意到您无法创建那么多线程。

Short answer: you can keep creating user threads until your JVM/OS can't handle any more. 简短回答:您可以继续创建用户线程,直到您的JVM / OS无法再处理。

Long answer: to quote from another answer I gave to this question : 答案很长:引用我对这个问题的另一个答案:

The term threads usually covers three abstraction layers: 术语线程通常包含三个抽象层:

  • User threads are threads launched by applications and are mapped N:M to: 用户线程是应用程序启动的线程,并将N:M映射到:
  • Kernel threads , which are threads managed by the operating system, mapped N:M to: 内核线程是由操作系统管理的线程 ,将N:M映射到:
  • Hardware threads , which are the actual physical resources available. 硬件线程 ,它们是可用的实际物理资源。

What you are creating in your application are User threads. 您在应用程序中创建的是用户线程。 As you can see, many user threads can map to a smaller number of hardware threads (the actual number of concurrent threads the hardware can handle, in this case 2). 如您所见,许多用户线程可以映射到较少数量的硬件线程(硬件可以处理的实际并发线程数,在本例中为2)。

The multiple layers that exist between the user threads and the lower levels apply their own scheduling mechanisms to move threads on and off the hardware cores, in order to enforce fairness, load-balancing or priority. 用户线程和较低级别之间存在的多个层应用它们自己的调度机制来在线程上移动线程,以实现公平性,负载平衡或优先级。

Threads are not tied to physical or logical processor cores. 线程不依赖于物理或逻辑处理器核心。 The operating system manages threads in a part called scheduler. 操作系统管理称为调度程序的部分中的线程。 Basically every thread gets a certain amount of time to run on a processor core then it gets paused and the next thread has time to run, after some time the first thread has again time to run. 基本上每个线程都会在处理器核心上运行一段时间,然后它会暂停并且下一个线程有时间运行,经过一段时间后第一个线程再次运行时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM