Principle

Use the higher-level concurrency utilities instead of wait and notify for easiness.

Use ConcurrentHashMap in preference to Collections.synchronizedMap or Hashtable.

Use concurrent collections in preference to externally synchronized collections.

Three categories of higher-level utilities in java.util.concurrent

  1. Executor Framework (Item 68)
  2. Concurrent collections - provide high- performance concurrent implementations of standard collection interfaces such as List, Queue, and Map.   

    Since all the implementation of Concurrent collections manage their own synchronization internally it's impossible to exclude concurrent activity from a concurrent collection; locking it will have no effect but slow the program.

// Method simulates the behavior of String.intern. Concurrent canonicalizing map atop ConcurrentMap - faster!

private static final ConcurrentMap<String, String> map = new ConcurrentHashMap<String, String>();

public static String intern(String s) {

String result = map.get(s);

if (result == null) {

result = map.putIfAbsent(s, s);

if (result == null)

result = s;

}

return result;

}

Note

String.intern must use some sort of weak reference to keep from leaking memory over time.

Blocking operation - wait until they can be successfully performed.

BlockingQueue (Used for work queues) extends Queue and adds several methods, including take, which removes and returns the head element from the queue, waiting if the queue is empty.

  1. Synchronizers - Objects that enable threads to wait for one another.(eg. CountDownLatch, Semaphore, CyclicBarrier and Exchanger).   

    Countdown latches are single-use barriers that allow one or more threads to wait for one or more other threads to do something.

    /**

    * Concurrency timer demo for "69 Prefer concurrency utilities to wait and notify".

    */

    package com.effectivejava.concurrency;

    import java.util.concurrent.CountDownLatch;

    import java.util.concurrent.Executor;

    import java.util.concurrent.SynchronousQueue;

    import java.util.concurrent.ThreadPoolExecutor;

    import java.util.concurrent.TimeUnit;

    /**

    * @author Kaibo Hao

    *

    */

    public class ExecutorManager {

    // Simple framework for timing concurrent execution

    public static long time(Executor executor, int concurrency,

    final Runnable action) throws InterruptedException {

    final CountDownLatch ready = new CountDownLatch(concurrency);

    final CountDownLatch start = new CountDownLatch(1);

    final CountDownLatch done = new CountDownLatch(concurrency);

    for (int i = 0; i < concurrency; i++) {

    executor.execute(new Runnable() {

    public void run() {

    ready.countDown(); // Tell timer we're ready

    try {

    start.await(); // Wait till peers are ready

    action.run();

    } catch (InterruptedException e) {

    Thread.currentThread().interrupt();

    } finally {

    done.countDown(); // Tell timer we're done

    }

    }

    });

    }

    ready.await(); // Wait for all workers to be ready

    long startNanos = System.nanoTime();

    start.countDown(); // And they're off!

    done.await(); // Wait for all workers to finish

    return System.nanoTime() - startNanos;

    }

    /**

    * @param args

    */

    public static void main(String[] args) {

    try {

    Executor executor = new ThreadPoolExecutor(0, 2, 10,

    TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>());

    long executedTime = time(executor, 2, new Runnable() {

    @Override

    public void run() {

    System.out.printf("Runing %s%n", Thread.currentThread());

    }

    });

    System.out.printf("%sns %.3fms %.3fs", executedTime,

    executedTime / 1000.0, executedTime / 1000000.0);

    } catch (InterruptedException e) {

    e.printStackTrace();

    }

    }

    }

Note

If a worker thread catches an InterruptedException, it reasserts the interrupt using the idiom Thread.currentThread().interrupt() and returns from its run method.

Since System.nanoTime is both more accurate and more precise, and it is not affected by adjustments to the system's real-time clock. For interval timing, always use System.nanoTime in preference to System.currentTimeMillis.

Always use the wait loop idiom to invoke the wait method; never invoke it outside of a loop.

// The standard idiom for using the wait method

synchronized (obj) {

while (<condition does not hold>)

obj.wait(); // (Releases lock, and reacquires on wakeup)

... // Perform action appropriate to condition

}

Reasons a thread might wake up when the condition does not hold:

• Another thread could have obtained the lock and changed the guarded state between the time a thread invoked notify and the time the waiting thread woke.

• Another thread could have invoked notify accidentally or maliciously when the condition did not hold. Classes expose themselves to this sort of mischief by waiting on publicly accessible objects. Any wait contained in a synchronized method of a publicly accessible object is susceptible to this problem.

• The notifying thread could be overly "generous" in waking waiting threads. For example, the notifying thread might invoke notifyAll even if only some of the waiting threads have their condition satisfied.

• The waiting thread could (rarely) wake up in the absence of a notify. This is known as a spurious wakeup[Posix, 11.4.3.6.1; JavaSE6].

Summary

using wait and notify directly is like programming in "concurrency assembly language," as compared to the higher-level language provided by java.util.concurrent. There is seldom, if ever, a reason to use wait and notify in new code. If you maintain code that uses wait and notify, make sure that it always invokes wait from within a while loop using the standard idiom. The notifyAll method should generally be used in preference to notify. If notify is used, great care must be taken to ensure liveness.

Effective Java 69 Prefer concurrency utilities to wait and notify的更多相关文章

  1. Effective Java 35 Prefer annotations to naming patterns

    Disadvantages of naming patterns Typographical errors may result in silent failures. There is no way ...

  2. Effective Java 53 Prefer interfaces to reflection

    Disadvantage of reflection You lose all the benefits of compile-time type checking, including except ...

  3. Effective Java 68 Prefer executors and tasks to threads

    Principle The general mechanism for executing tasks is the executor service. If you think in terms o ...

  4. Effective Java 18 Prefer interfaces to abstract classes

    Feature Interface Abstract class Defining a type that permits multiple implementations Y Y Permitted ...

  5. Effective Java 20 Prefer class hierarchies to tagged classes

    Disadvantage of tagged classes 1. Verbose (each instance has unnecessary irrelevant fields). 2. Erro ...

  6. Effective Java 25 Prefer lists to arrays

    Difference Arrays Lists 1 Covariant Invariant 2 Reified at runtime Erased at run time 3 Runtime type ...

  7. Effective Java 46 Prefer for-each loops to traditional for loops

    Prior to release 1.5, this was the preferred idiom for iterating over a collection: // No longer the ...

  8. Effective Java 49 Prefer primitive types to boxed primitives

    No. Primitives Boxed Primitives 1 Have their own values Have identities distinct from their values 2 ...

  9. 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 ...

随机推荐

  1. SystemTap知识(一)

    SystemTap是一个系统的跟踪探测工具.它能让用户来跟踪和研究计算机系统在底层的实现. 安装SystemTap需要为你的系统内核安装-devel,-debuginfo,-debuginfo-com ...

  2. iOS实现书架布局样式【一些电子书的首页】

    本文实现了类似电子书首页,用来展示图书或小说的布局页面,书架列表[iPhone6模拟器],屏幕尺寸还没进行适配,只是做个简单的demo[纯代码实现方式] 实现采用的是UICollectionView和 ...

  3. WPF Control Hints - TabControl : 怎么修改整个tab header的margin?

    WFP里面TabControl我们可以添加多个TabItem,每个TabItem的Header就是我们常点击的tab标签.但是默认的layout行为里面,这个header是有个2个像素的margin, ...

  4. 循序渐进开发WinForm项目(1) --数据库设计和项目框架的生成

    随笔背景:在很多时候,很多入门不久的朋友都会问我:我是从其他语言转到C#开发的,有没有一些基础性的资料给我们学习学习呢,你的框架感觉一下太大了,希望有个循序渐进的教程或者视频来学习就好了. 其实也许我 ...

  5. SqlServer一张表数据导入另一张表,收藏使用,工作中更新数据错误很有用

    sql一张表数据导入另一张表   1.如果2张表的字段一致,并且希望插入全部数据,可以用这种方法:   INSERT INTO 目标表 SELECT * FROM 来源表;   2.比如要将 arti ...

  6. csharp: Export or Import excel using MyXls,Spire.Xls

    excel 2003 (效果不太理想) using System; using System.Collections.Generic; using System.ComponentModel; usi ...

  7. poi excel export 乱码

    1. Question Description: ~前端是get方式提交的,参数含有中文“测试” ~导出的excel,文件名正常, 而标题乱码 2. Solution: ~断点发现, 参数接收就是乱码 ...

  8. [Xamarin.Android] Support Library Tips

    [Xamarin.Android] Support Library Tips Support Library支持内容 Xamarin Support Library每个版本支持.那些组件,可以参考这份 ...

  9. StackOverflow Update: 560M Pageviews A Month, 25 Servers, And It's All About Performance

    http://highscalability.com/blog/2014/7/21/stackoverflow-update-560m-pageviews-a-month-25-servers-and ...

  10. ASP.NET MVC:窗体身份验证及角色权限管理示例

    ASP.NET MVC 建立 ASP.NET 基础之上,很多 ASP.NET 的特性(如窗体身份验证.成员资格)在 MVC 中可以直接使用.本文旨在提供可参考的代码,不会涉及这方面太多理论的知识. 本 ...