Principle

  • Any program that relies on the thread scheduler for correctness or performance is likely to be nonportable.

    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.

    Note

    The number of runnable threads isn't the same as the total number of threads, which can be much higher. Threads that are waiting are not runnable.

  • Threads should not run if they aren't doing useful work.

    // Awful CountDownLatch implementation - busy-waits incessantly!

    public class SlowCountDownLatch {

    private int count;

    public SlowCountDownLatch(int count) {

    if (count < 0)

    throw new IllegalArgumentException(count + " < 0");

    this.count = count;

    }

    public void await() {

    while (true) {

    synchronized(this) {

    if (count == 0) return;

    }

    }

    }

    public synchronized void countDown() {

    if (count != 0)

    count--;

    }

    }

  • 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 priorities are among the least portable features of the Java platform.

    Summary

    Do not depend on the thread scheduler for the correctness of your program. The resulting program will be neither robust nor portable. As a corollary, do not rely on Thread.yield or thread priorities. These facilities are merely hints to the scheduler. Thread priorities may be used sparingly to improve the quality of service of an already working program, but they should never be used to "fix" a program that barely works.

Effective Java 72 Don't depend on the thread scheduler的更多相关文章

  1. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

  2. 《Effective Java》读书笔记 - 10.并发

    Chapter 10 Concurrency Item 66: Synchronize access to shared mutable data synchronized这个关键字不仅保证了同步,还 ...

  3. Effective Java 目录

    <Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...

  4. 【Effective Java】阅读

    Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...

  5. Effective Java通俗理解(下)

    Effective Java通俗理解(上) 第31条:用实例域代替序数 枚举类型有一个ordinal方法,它范围该常量的序数从0开始,不建议使用这个方法,因为这不能很好地对枚举进行维护,正确应该是利用 ...

  6. 《Effective Java(中文第二版)》【PDF】下载

    <Effective Java(中文第二版)>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230382186 Java(中文第二版)& ...

  7. Effective Java 第三版——49. 检查参数有效性

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  8. 《Effective Java》 学习笔记 —— 并发

    <Effective Java>第二版学习笔记之并发编程. 第66条 同步访问共享的可变数据 * 关键字synchronized可以保证在同一时刻只有一个线程可以执行某个方法或代码块. * ...

  9. Java 高效编程(Effective Java)中文第三版(补档)

    来源:sjsdfg/effective-java-3rd-chinese <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过, ...

随机推荐

  1. Twitter算法面试题详解(Java实现)

    最近在网上看到一道Twitter的算法面试题,网上已经有人给出了答案,不过可能有些人没太看明白(我也未验证是否正确),现在给出一个比较好理解的答案.先看一下题目. 图1 先看看图图1.可以将方块看做砖 ...

  2. [Solution] 一步一步WCF(1) 快速入门

    Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台.整合了原有的windows通讯的 .n ...

  3. 团队项目2.0软件改进分析MathAPP

    软件改进分析 在此基础上,进行软件的改进. 首先,我们把这个软件理解成一个投入市场的.帮助小朋友进行算术运算练习的APP. 从质量保证的角度,有哪些需要改进的BUG? 从用户的角度(把自己当成小学生或 ...

  4. C#设计模式——代理模式(Proxy Pattern)

    一.概述在软件开发中,有些对象由于创建成本高.访问时需要与其它进程交互等原因,直接访问会造成系统速度慢.复杂度增大等问题.这时可以使用代理模式,给系统增加一层间接层,通过间接层访问对象,从而达到隐藏系 ...

  5. ViewData和TempData以及Session的小结

    ViewData一般用在从控制器向页面上传递数据. Public ActionResult Show() { ViewData["message"]="你好"; ...

  6. 在IIS7中使用ARR(Application Request Routing)反向代理虚拟目录到Nodejs站点

    目标: 1.访问www.arrdemo.com/proxy 跳转到 localhost:8898的Nodejs站点 2.Nodejs站点的页面可以返回到浏览器,包括js,css,图片 3.Nodejs ...

  7. 获取datatable更新之前的数据

    string dd = ds.Tables[0].Rows[0][0, DataRowVersion.Original].ToString() ;

  8. [moka同学笔记]yii2.0 advanced高级版 安装配置 与 rbac (Ⅰ)

    1.下载地址:http://www.yiichina.com/download,下载 Yii2 的高级应用程序模板 2.配置与安装 在服务器www目录下yii2test  [下载下来更改advance ...

  9. Erlang进程间消息接收超时设定

        Erlang消息接收函数,一般都会设计成尾递归调用自己的模式.但是这样的模式,如果没有消息则会无限的等待下去,所以为了不无限等待,这里可以加个超时设定,例如: flush() -> re ...

  10. Smtp邮件发送系统公用代码整理—总结

    1.前言 a.在软件开发中,我们经常能够遇到给用户或者客户推送邮件,推送邮件也分为很多方式,比如:推送一句话,推送一个网页等等.那么在系统开发中我们一般在什么情况下会使用邮件发送呢?下面我简单总结了一 ...