`
conkeyn
  • 浏览: 1524647 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

Java threads may not use all your CPUs

阅读更多

Enterprise-level applications often need more CPU power than a single-CPU machine can provide, but getting applications to effectively use multiple CPUs can be tricky. If you're lucky, you may be able to run an application on separate single-CPU machines; Web servers scale this way. But many applications, such as databases, need to run on a single machine, which will require additional CPUs to be scaled.

In this article, we'll examine tests that show that Java threads in Sun's JRE 1.1.7 do not use more than two processors by default, regardless of how many are in the machine. Java is advertised as automatically scaling to use all available processors, but we'll see below that this is not entirely true. We'll also look at some simple ways to maximize Java multithreading performance on multi-CPU machines.

Note: My tests indicate that JRE 1.1.7 does not use more than two CPUs when running pure Java programs. Under other conditions, such as with native code that has native OS-level threads, JRE 1.1.7 may utilize all of the CPUs.

The scoop on symmetric multiprocessing

Getting multiple equivalent CPUs to cooperate in one machine is known as symmetric multiprocessing (SMP); running a single-CPU machine is known as uniprocessing. SMP machines' CPU modules usually plug into a bus. In theory, you add capacity by plugging in more CPUs.

Unfortunately, because coordinating CPU activity is difficult and most applications spend a lot of time waiting for I/O rather than using the CPU, using two processors doesn't necessarily double your throughput. Performance may even degrade with multiple CPUs. Indeed, for software not designed for an SMP system, adding a CPU may increase performance by no more than 30 percent.

For CPU-intensive programs designed for SMP, a second CPU may increase performance by 80 or 90 percent, depending on what the application is doing. CPU-intensive programs that can run as parallel processes or threads benefit more from additional CPU power than programs that spend most of their CPU time idle, waiting for a network or disk.

SMP options

One way to use multiple CPUs on a single machine is to run multiple processes that communicate via the various forms of interprocess communication, such as semaphores and shared memory. The operating system will automatically allocate different CPUs to different processes. Interprocess communication techniques are not especially portable, though. One alternative is using native thread programming in C or C++: this is difficult, but is a way to use multiple CPUs in a single process with better portability.

Java thread programming is simple by comparison and provides high portability. Java threads have also been advertised as scaling well on multi-CPU machines. You can run Java threads in parallel on multiple CPUs, but only if you use a native-threads library for Java instead of the green threads option. (The green thread implementation does all thread scheduling within the JVM. On Unix, that means all threads will run in a single process and never use multiple CPUs.)

分享到:
评论

相关推荐

    Java Threads

    《Java Threads》是Java并发编程领域的一本经典著作,由Scott Oaks和Doug Lea合作撰写,由O'Reilly出版社出版。这本书深入浅出地讲解了Java平台上的多线程编程技术,是Java开发者深入理解并发编程的必备参考资料。在...

    Java Threads Third Edition.chm

    Java Threads Third Edition.chm

    linux实用脚本show-busy-java-threads

    本文将深入探讨标题所提及的三个实用脚本:“show-busy-java-threads”、“show-duplicate-java-classes”以及“find-in-jars”。这些脚本都是针对Java开发者和系统管理员的利器,旨在提高效率和解决问题。 1. **...

    show-busy-java-threads.sh文件

    `show-busy-java-threads.sh`脚本就是为了帮助开发者快速定位和排查这类性能问题而设计的。这个脚本主要用于监控并展示Java应用程序中的繁忙线程,从而帮助我们理解程序的执行状态,找出可能导致高CPU负载的原因。 ...

    Java.Threads.and.the.Concurrency.Utilities.1484216997

    Threads and the Concurrency Utilities helps all Java developers master and use these capabilities effectively. This book is divided into two parts of four chapters each. Part 1 focuses on the Thread...

    Java.Threads.3rd.Edition(chm)

    This book is intended for programmers of all levels who need to learn to use threads within Java programs. This includes developers who have previously used Java and written threaded programs; J2SE ...

    java Threads

    Java线程是多任务编程的核心概念,特别是在Java这种支持并发执行的语言中。本文将深入探讨Java线程的创建、管理以及相关同步机制,基于提供的压缩包文件中的章节名称,我们可以推测这些章节可能涵盖线程的基本概念到...

    Java Threads and the Concurrency Utilities

    《Java多线程与并发工具》一书深入讲解了Java中线程API和并发工具的使用,这些内容是Java语言中最强大但也最具挑战性的API和语言特性之一。对于初学者而言,利用这些特性编写正确的多线程应用程序通常是非常困难的。...

    show-busy-java-threads

    上传到 linux线上服务器,执行 chmod -R 777 show-busy-java-threads 执行权限。 ./show-busy-java-threads 查看执行占用cpu偏高的 线程,方便我们排查定位。

    Java Threads and the Concurrency Utilities(Apress,2015)

    Threads and the Concurrency Utilities helps all Java developers master and use these capabilities effectively. This book is divided into two parts of four chapters each. Part 1 focuses on the Thread...

    Java.Threads.3rd.Edition.txt

    根据提供的文件信息,我们可以推断出这是一本关于Java线程技术的书籍——《Java Threads 第三版》。下面将从书中的各个章节提取并总结关键知识点。 ### 一、绪论 #### 5.31.3 为什么需要线程? 在现代编程中,...

    Java Threads Third Edition

    Threads are essential to Java programming, but learning to use them effectively is a nontrivial task. This new edition of the classic Java Threads shows you how to take full advantage of Java‘s ...

    Java Threads 2rd

    ### Java Threads 2rd #### 书名:Java Threads 2rd 本书由Scott Oaks与Henry Wong合著,是关于Java线程技术的权威指南。它深入探讨了Java中的线程概念、API以及如何在多处理器环境中高效地利用线程进行编程。 ##...

    APress-Taming-Java-Threads-Code.rar_taming java threads_threads

    《驯服Java线程》是Apress出版的一本关于Java多线程编程的书籍,它深入探讨了如何在Java环境中有效地管理和控制线程,以优化程序性能和稳定性。线程在现代软件开发中扮演着至关重要的角色,尤其是在并发处理和高并发...

    show-busy-java-threads.sh

    java线程定位脚本

    Java Threads and the Concurrency Utilities 无水印pdf

    Java Threads and the Concurrency Utilities 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权...

    Java Threads, 3rd Edition, Scott Oaks and Henry Wong

    Java Threads, Third Edition, has been thoroughly expanded and revised. It incorporates the con-currency utilities from java.util.concurrent throughout. New chapters cover thread performance, using ...

    Introduction to Java Threads

    ### Java线程介绍 #### 关于本教程 本教程旨在为Java程序员提供关于线程的基础知识,包括线程的概念、用途以及如何编写简单的多线程程序。此外,本教程还会探讨更复杂的线程应用程序的基本构建模块,如在不同线程...

Global site tag (gtag.js) - Google Analytics