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. 适用于jquery1.11.1的ajaxfileupload.js

    ajaxfileupload源码 解决上传成功不走success的问题 解决高版本jquery兼容性问题 jQuery.extend({ createUploadIframe: function(id ...

  2. SQL Server 文件和文件组

    文件和文件组简介 在SQL Server中,数据库在硬盘上的存储方式和普通文件在Windows中的存储方式没有什么不同,仅仅是几个文件而已.SQL Server通过管理逻辑上的文件组的方式来管理文件. ...

  3. sql server2008中怎样用sql语句创建数据库和数据表

    这是简单用代码实现创建数据库和数据表的sql语句,如下: --调用系统数据库-- use master go /***防止你要创建的数据库同名,先把它删除掉****/ if Exists(select ...

  4. 划分树---Feed the dogs

    POJ  2761 Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to fee ...

  5. 爱你.一万年>>数据库基础

    嗨!最近学习了一项新的技能,学习了数据库的知识,在这里抛砖引玉,给大家说说,望赐教!!>> 01.首先给大家谈谈为什么要使用数据库? 数据库可以看成是一个永久保存数据的仓库. 可以存储大量 ...

  6. 【iOS】Quartz2D练习-动态改变属性值

    一.通过slider控制圆的缩放 1.实现过程 新建一个项目,新建一个继承自UIview的类,并和storyboard中自定义的view进行关联.代码示例:SLViewController.m文件 # ...

  7. 面向企业客户的制造业CRM系统的不成熟思考

    CRM就是客户关系管理(Customer Relationship Management),一直一知半解,最近有涉及这方面的需求,所以稍作研究,并思考一些相关问题. CRM是什么? CRM具体如何定义 ...

  8. [小北De编程手记] : Lesson 01 - Selenium For C# 之 环境搭建

    在我看来一个自动化测试平台的构建,是一种很好的了解开发语言,单元测试框架,自动化测试驱动,设计模式等等等的途径.因此,在下选择了自动化测试的这个话题来和大家分享一下本人关于软件开发和自动化测试的认识. ...

  9. sublimeCodeIntel 的配置

    在项目的根目录目录下建立.codeintel/config 但是在windows 需要进入dos 环境下建立.以点开头的文件夹和文件.资源管理器不允许创建点开头的文件或文件夹,但在命令提示符下是可以的 ...

  10. 【JWPlayer】官方JWPlayer去水印步骤

    在前端播放视频,现在用html5的video标签已经是一个不错的选择,不过有时候还是需要用StrobeMediaPlayback.JWPlayer这一类的flash播放器,JWPlayer的免费版本带 ...