`
leonzhx
  • 浏览: 793330 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Item 72: Don't depend on the thread scheduler

阅读更多

1.  The best way to write a robust, responsive, portable program is to ensure that the average number of runnable threads is not significantly greater than the number of processors. This leaves the thread scheduler with little choice: it simply runs the runnable threads till they’re no longer runnable. The program’s behavior doesn’t vary too much, even under radically different thread-scheduling policies.

 

2.  The main technique for keeping the number of runnable threads down is to have each thread do some useful work and then wait for more. Threads should not run if they aren’t doing useful work. In terms of the Executor Framework, this means sizing your thread pools appropriately, and keeping tasks reasonably small and independent of one another. Tasks shouldn’t be too small, or dispatching overhead will harm performance.

 

3.  Threads should not busy-wait, repeatedly checking a shared object waiting for something to happen. Besides making the program vulnerable to the vagaries of the scheduler, busy-waiting greatly increases the load on the processor, reducing the amount of useful work that others can accomplish.

 

4.  When faced with a program that barely works because some threads aren’t getting enough CPU time relative to others, resist the temptation to “fix” the program by putting in calls to Thread.yield. Thread.yield has no testable semantics. A better course of action is to restructure the application to reduce the number of concurrently runnable threads. 

 

5.  Thread priorities are among the least portable features of the Java platform. It is not unreasonable to tune the responsiveness of an application by tweaking a few thread priorities, but it is rarely necessary and is not portable. It is unreasonable to solve a serious liveness problem by adjusting thread priorities.

 

6.  It is within specification for Thread.yield to do nothing at all, simply returning control to its caller. Some modern VMs actually do this. Therefore, you should use Thread.sleep(1) instead of Thread.yield for concurrency testing.

 

分享到:
评论

相关推荐

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    Item 84: Don’t depend on the thread scheduler 12 Serialization Item 85: Prefer alternatives to Java serialization Item 86: Implement Serializable with great caution Item 87: Consider using a custom ...

    nRF 52832 SoftDeive_s132_nrf52_4.0.4

    s132_nrf52_4.0.4 This is a production release that contains minor but important changes to the 4.0.3 release. ... The actual requirements depend on the configuration chosen at sd_ble_enable() time.

    xlCompiler 3.1

    Your application doesn't depend on the version of Microsoft Excel installed on the user's PC. Also, it doesn't depend on the Add-ins and other software installed on user's PC, which could slow down ...

    高中英语 知识点大全44 demand、depend on的用法

    "I don’t want to depend on my parents any longer"则表达了个人独立的愿望,不再依赖父母。"depend on"的另一个常见表达是"rely on",两者的含义相同,都强调了对某人或某事的信赖。 此外,我们还提到了"no more...

    Effective C#

    - **Purpose:** `readonly` variables are initialized once and can't be changed, making them suitable for constants that may depend on runtime information. - **Usage:** Use `readonly` for values that ...

    what-do-i-depend-on:看看你所依赖的

    $ npm install -g what-do-i-depend-on 用法 输入完整命令: $ what-do-i-depend-on 要不就: $ wdido wdido需要选项。 Usage : what-do-i-depend-on [path] [options] --dependencies, --pro Show only ...

    译林九年级英语unitunit知识点总结大全PPT学习教案.pptx

    - `give up`:放弃,例如:Don't give up easily, keep trying. - `sales department`:销售部,例如:I work in the sales department of our company. - `work with`:与谁工作,例如:I enjoy working with my...

    Android代码-MyOwnNotes

    This application does not depend on the server side owncloud notes app anymore. However, it does depend on the ownCloud android application_ for authentication. MyOwnNotes 2 has not been released on ...

    ANT Package Script For Depend On Tomcat Developing

    标题 "ANT Package Script For Depend On Tomcat Developing" 暗示了这个压缩包涉及的是使用Ant工具进行依赖Tomcat的项目构建与打包的过程。Ant是一个Java语言编写的开源构建工具,常用于自动化Java项目的构建、打包...

    依赖库分许工具

    依赖库分析工具Depend是一个非常实用的软件,主要用于解析Windows程序在运行时所依赖的各种动态链接库(Dynamic Link Libraries, DLLs)和其他系统组件。它能够帮助开发者和系统管理员了解程序运行所需的环境,确保...

    新东方六级高频词组.doc

    2. depend on - 依赖,例如:Children depend on their parents for support. 3. devote to - 投入,致力于,例如:She devotes herself to her work. E部分: 1. engage in - 参与或忙于,例如:They engaged in a...

    高中英语动词短语分类.doc

    19. **depend on**:依靠或依赖,如:Children depend on their parents for support. 20. **die for/die from/of**:为…而死或因…而死,如:Many soldiers died for their country. / He died from a heart ...

    高考英语140个基础动词例句.doc

    例句:Don't forget to bring your homework tomorrow. 20. borrow(借入):暂时从他人那里获取物品。 例句:Can I borrow your pen for a moment? 21. buy(购买):用钱交换商品或服务。 例句:She went ...

    常用英文同意词组表达.doc

    - `depend on` 和 `depend upon` 表示“依靠,信赖”,如:I depend on you for help. - `rely on sb` 或 `rely upon sb` 同样表示“依赖某人”,如:We rely on our team members to complete the project. 2. *...

    dll depend

    "dll depend"是一个专门用于分析DLL文件依赖关系的工具,它可以帮助开发者找出程序运行时可能遇到的错误或缺失的库。 `depends.dll`和`depends.exe`是这个工具的核心组件,`depends.exe`是主执行文件,用于用户界面...

    高中英语 知识点大全109 remain

    - "depend on" 强调对他人的“依靠”,如:“We depend on rain for water.”(我们依赖雨水获取水源。) - "trust (in)" 指的是深度的“信赖”,相信某人不会让你失望,如:“I trust him completely.”(我完全...

    初中中考复习易混淆动词短语汇总及练习.doc

    30. ** Be late for…** - 迟到,如:Don't be late for the meeting. 31. ** Be made of (from)** - 由...制成,如:The table is made of wood. 32. ** Be satisfied with** - 对...感到满意,如:She was ...

    matlab开发-makedepend

    在MATLAB开发过程中,`makedepend`是一个非常重要的工具,尤其对于大型项目而言,它有助于管理和维护代码的依赖关系。这个工具能够自动检测`.m`脚本或函数文件中引用的所有其他函数和变量,从而生成一个依赖文件,...

    orebuild:给定 .depend 和 change mlmli 文件,计算所有需要重新编译的 mlmli 文件

    给定 .depend(单行格式)和已更改的 ml/mli 文件列表,计算所有需要重新编译的 ml/mli 文件。 这可用于通过避免重复调用 ocamlc/ocamlopt 来加速 ocaml 构建。 orebuild还可用于简化 Makefile。 要求:ocamlscript

Global site tag (gtag.js) - Google Analytics