Oracle官方并发教程(2)

来源:互联网 发布:游戏超人软件 编辑:程序博客网 时间:2024/04/29 23:37


保护块


多线程之间经常需要协同工作,最常见的方式是使用保护块(Guarded Blocks),它循环检查一个条件(通常初始值为true),直到条件发生变化才跳出循环继续执行。在使用Guarded Blocks时有以下几个步骤需要注意:

假设guardedJoy()方法必须要等待另一线程为共享变量joy设值才能继续执行。那么理论上可以用一个简单的条件循环来实现,但在等待过程中guardedJoy方法不停的检查循环条件实际上是一种资源浪费。

public void guardedJoy() {    // Simple loop guard. Wastes    // processor time. Don't do this!    while(!joy) {}    System.out.println("Joy has been achieved!");}

更加高效的方法是调用Object.wait将当前线程挂起,直到有另一线程发起事件通知(尽管通知的事件不一定是当前线程等待的事件)。

public synchronized void guardedJoy() {    // This guard only loops once for each special event, which may not    // be the event we're waiting for.    while(!joy) {        try {            wait();        } catch (InterruptedException e) {}    }    System.out.println("Joy and efficiency have been achieved!");}

注意:一定要在循环里面调用wait方法,不要想当然的认为线程唤醒后循环条件一定发生了改变。(译注:这样可以保证若循环条件没改变,当前线程将重新挂起。)

和其他可以暂停线程执行的方法一样,wait方法会抛出InterruptedException,在上面的例子中,因为我们关心的是joy的值,所以忽略了InterruptedException。

为什么guardedJoy是synchronized方法?假设d是用来调用wait的对象,当一个线程调用d.wait,它必须要拥有d的内部锁(否则会抛出异常),获得d的内部锁的最简单方法是在一个synchronized方法里面调用wait。

当一个线程调用wait方法时,它释放锁并挂起。然后另一个线程请求并获得这个锁并调用Object.notifyAll通知所有等待该锁的线程。

public synchronized notifyJoy() {    joy = true;    notifyAll();}

当第二个线程释放这个该锁后,第一个线程再次请求该锁,从wait方法返回并继续执行。

注意:还有另外一个通知方法,notify(),它只会唤醒一个线程。但由于它并不允许指定哪一个线程被唤醒,所以一般只在大规模并发应用(即系统有大量相似任务的线程)中使用。因为对于大规模并发应用,我们其实并不关心哪一个线程被唤醒。

现在我们使用Guarded blocks创建一个生产者/消费者应用。这类应用需要在两个线程之间共享数据:生产者生产数据,消费者使用数据。两个线程通过共享对象通信。在这里,线程协同工作的关键是:生产者发布数据之前,消费者不能够去读取数据;消费者没有读取旧数据前,生产者不能发布新数据。

在下面的例子中,数据是一系列文本消息,通过Drop类型的对象来共享:

public class Drop {    // Message sent from producer to consumer.    private String message;    // True if consumer should wait for producer to send message,    // false if producer should wait for consumer to retrieve message.    private boolean empty = true;    public synchronized String take() {        // Wait until message is available.        while (empty) {            try {                wait();            } catch (InterruptedException e) {}        }        // Toggle status.        empty = true;        // Notify producer that status has changed.        notifyAll();        return message;    }    public synchronized void put(String message) {        // Wait until message has been retrieved.        while (!empty) {            try {                 wait();            } catch (InterruptedException e) {}        }        // Toggle status.        empty = false;        // Store message.        this.message = message;        // Notify consumer that status has changed.        notifyAll();    }}
Producer是生产者线程,发送一组消息,字符串DONE表示所有消息都已经发送完成。为了模拟现实情况,生产者线程还会在消息发送时随机的暂停。

import java.util.Random;public class Producer implements Runnable {    private Drop drop;    public Producer(Drop drop) {        this.drop = drop;    }    public void run() {        String importantInfo[] = {            "Mares eat oats",            "Does eat oats",            "Little lambs eat ivy",            "A kid will eat ivy too"        };        Random random = new Random();        for (int i = 0; i < importantInfo.length; i++) {            drop.put(importantInfo[i]);            try {                Thread.sleep(random.nextInt(5000));            } catch (InterruptedException e) {}        }        drop.put("DONE");    }}
Consumer是消费者线程,读取消息并打印出来,直到读取到字符串DONE为止。消费者线程在消息读取时也会随机的暂停。
import java.util.Random;public class Consumer implements Runnable {    private Drop drop;    public Consumer(Drop drop) {        this.drop = drop;    }    public void run() {        Random random = new Random();        for (String message = drop.take(); ! message.equals("DONE"); message = drop.take()) {            System.out.format("MESSAGE RECEIVED: %s%n", message);            try {                Thread.sleep(random.nextInt(5000));            } catch (InterruptedException e) {}        }    }}
下面是主线程ProducerConsumerExample,用于启动Producer和Consumer线程。

public class ProducerConsumerExample {    public static void main(String[] args) {        Drop drop = new Drop();        (new Thread(new Producer(drop))).start();        (new Thread(new Consumer(drop))).start();    }}
注意:Drop类是用来演示Guarded Blocks如何工作的。为了避免重新发明轮子,当你尝试创建自己的共享数据对象时,请查看Java Collections Framework中已有的数据结构。如需更多信息,请参考Questions and Exercises。


不可变对象


一个对象如果在创建后不能被修改,那么就称为不可变对象。在并发编程中,一种被普遍认可的原则就是:尽可能的使用不可变对象来创建简单、可靠的代码。

在并发编程中,不可变对象特别有用。由于创建后不能被修改,所以不会出现由于线程干扰产生的错误或是内存一致性错误。
但是程序员们通常并不热衷于使用不可变对象,因为他们担心每次创建新对象的开销。实际上这种开销常常被过分高估,而且使用不可变对象所带来的一些效率提升也抵消了这种开销。例如:使用不可变对象降低了垃圾回收所产生的额外开销,也减少了用来确保使用可变对象不出现并发错误的一些额外代码。
接下来看一个可变对象的类,然后转化为一个不可变对象的类。通过这个例子说明转化的原则以及使用不可变对象的好处。

一个同步类的例子

SynchronizedRGB是表示颜色的类,每一个对象代表一种颜色,使用三个整形数表示颜色的三基色,字符串表示颜色名称。
public class SynchronizedRGB {    // Values must be between 0 and 255.    private int red;    private int green;    private int blue;    private String name;    private void check(int red, int green, int blue) {        if (red < 0 || red > 255 ||            green < 0 || green > 255 ||            blue < 0 || blue > 255) {            throw new IllegalArgumentException();        }    }    public SynchronizedRGB(int red, int green, int blue, String name) {        check(red, green, blue);        this.red = red;        this.green = green;        this.blue = blue;        this.name = name;    }    public void set(int red, int green, int blue, String name) {        check(red, green, blue);        synchronized (this) {            this.red = red;            this.green = green;            this.blue = blue;            this.name = name;        }    }    public synchronized int getRGB() {        return ((red << 16) | (green << 8) | blue);    }    public synchronized String getName() {        return name;    }    public synchronized void invert() {        red = 255 - red;        green = 255 - green;        blue = 255 - blue;        name = "Inverse of " + name;    }}

使用SynchronizedRGB时需要小心,避免其处于不一致的状态。例如一个线程执行了以下代码:

SynchronizedRGB color = new SynchronizedRGB(0, 0, 0, "Pitch Black");...int myColorInt = color.getRGB();      //Statement 1String myColorName = color.getName(); //Statement 2
如果有另外一个线程在Statement 1之后、Statement 2之前调用了color.set方法,那么myColorInt的值和myColorName的值就会不匹配。为了避免出现这样的结果,必须要像下面这样把这两条语句绑定到一块执行:
synchronized (color) {    int myColorInt = color.getRGB();    String myColorName = color.getName();} 

这种不一致的问题只可能发生在可变对象上。

定义不可变对象的策略

以下的一些规则是创建不可变对象的简单策略。并非所有不可变类都完全遵守这些规则,不过这不是编写这些类的程序员们粗心大意造成的,很可能的是他们有充分的理由确保这些对象在创建后不会被修改。但这需要非常复杂细致的分析,并不适用于初学者。

  1. 不要提供setter方法。(包括修改字段的方法和修改字段引用对象的方法)
  2. 将类的所有字段定义为final、private的。
  3. 不允许子类重写方法。简单的办法是将类声明为final,更好的方法是将构造函数声明为私有的,通过工厂方法创建对象。
  4. 如果类的字段是对可变对象的引用,不允许修改被引用对象。
    • 不提供修改可变对象的方法。
    • 不共享可变对象的引用。当一个引用被当做参数传递给构造函数,而这个引用指向的是一个外部的可变对象时,一定不要保存这个引用。如果必须要保存,那么创建可变对象的拷贝,然后保存拷贝对象的引用。同样如果需要返回内部的可变对象时,不要返回可变对象本身,而是返回其拷贝。

将这一策略应用到SynchronizedRGB有以下几步:

  1. SynchronizedRGB类有两个setter方法。第一个set方法只是简单的为字段设值(译者注:删掉即可),第二个invert方法修改为创建一个新对象,而不是在原有对象上修改。
  2. 所有的字段都已经是私有的,加上final即可。
  3. 将类声明为final的。
  4. 只有一个字段是对象引用,并且被引用的对象也是不可变对象。

经过以上这些修改后,我们得到了ImmutableRGB:

final public class ImmutableRGB {    // Values must be between 0 and 255.    final private int red;    final private int green;    final private int blue;    final private String name;    private void check(int red, int green, int blue) {        if (red < 0 || red > 255 ||            green < 0 || green > 255 ||            blue < 0 || blue > 255) {            throw new IllegalArgumentException();        }    }    public ImmutableRGB(int red, int green, int blue, String name) {        check(red, green, blue);        this.red = red;        this.green = green;        this.blue = blue;        this.name = name;    }    public int getRGB() {        return ((red << 16) | (green << 8) | blue);    }    public String getName() {        return name;    }    public ImmutableRGB invert() {        return new ImmutableRGB(255 - red,                       255 - green,                       255 - blue,                       "Inverse of " + name);    }}


高级并发对象


目前为止,该教程重点讲述了最初作为Java平台一部分的低级别API。这些API对于非常基本的任务来说已经足够,但是对于更高级的任务就需要更高级的API。特别是针对充分利用了当今多处理器和多核系统的大规模并发应用程序。本节,我们将着眼于Java 5.0新增的一些高级并发特征。大多数特征已经在新的java.util.concurrent包中实现。Java集合框架中也定义了新的并发数据结构。

  • 锁对象提供了可以简化许多并发应用的锁的惯用法。
  • 执行器Executors为加载和管理线程定义了高级API。Executors的实现由java.util.concurrent包提供,提供了适合大规模应用的线程池管理。
  • 并发集合简化了大型数据集合管理,且极大的减少了同步的需求。
  • 原子变量有减小同步粒度和避免内存一致性错误的特征。
  • 并发随机数(JDK7)提供了高效的多线程生成伪随机数的方法。

锁对象

同步代码依赖于一种简单的可重入锁。这种锁使用简单,但也有诸多限制。java.util.concurrent.locks包提供了更复杂的锁。我们不会详细考察这个包,但会重点关注其最基本的接口,锁。 锁对象作用非常类似同步代码使用的隐式锁。如同隐式锁,每次只有一个线程可以获得锁对象。通过关联Condition对象,锁对象也支持wait/notify机制。 锁对象之于隐式锁最大的优势在于,它们有能力收回获得锁的尝试。如果当前锁对象不可用,或者锁请求超时(如果超时时间已指定),tryLock方法会收回获取锁的请求。如果在锁获取前,另一个线程发送了一个中断,lockInterruptibly方法也会收回获取锁的请求。 让我们使用锁对象来解决我们在活跃度中见到的死锁问题。Alphonse和Gaston已经把自己训练成能注意到朋友何时要鞠躬。我们通过要求Friend对象在双方鞠躬前必须先获得锁来模拟这次改善。下面是改善后模型的源代码,Safelock。为了展示其用途广泛,我们假设Alphonse和Gaston对于他们新发现的能够安全鞠躬的能力是如此入迷,以至于他们一直在相互鞠躬(译注:在循环中不停地执行鞠躬)。

import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;import java.util.Random;public class Safelock {    static class Friend {        private final String name;        private final Lock lock = new ReentrantLock();        public Friend(String name) {            this.name = name;        }        public String getName() {            return this.name;        }                //准备鞠躬        public boolean impendingBow(Friend bower) {            Boolean myLock = false;            Boolean yourLock = false;            try {                myLock = lock.tryLock();  //获取自己的锁                yourLock = bower.lock.tryLock();  //获取对方的锁            } finally {                if (!(myLock && yourLock)) {                    if (myLock) {                        lock.unlock();                    }                    if (yourLock) {                        bower.lock.unlock();                    }                }            }            return myLock && yourLock;        }        public void bow(Friend bower) {            if (impendingBow(bower)) {                try {                    System.out.format("%s: %s has"                            + " bowed to me!%n",                            this.name, bower.getName());                    bower.bowBack(this);                } finally {                    lock.unlock();                    bower.lock.unlock();                }            } else {                System.out.format("%s: %s started"                        + " to bow to me, but saw that"                        + " I was already bowing to"                        + " him.%n",                        this.name, bower.getName());            }        }        public void bowBack(Friend bower) {            System.out.format("%s: %s has"                    + " bowed back to me!%n",                    this.name, bower.getName());        }    }    static class BowLoop implements Runnable {        private Friend bower;        private Friend bowee;        public BowLoop(Friend bower, Friend bowee) {            this.bower = bower;            this.bowee = bowee;        }        @Override        public void run() {            Random random = new Random();            for (;;) {                try {                    Thread.sleep(random.nextInt(10));                } catch (InterruptedException e) {                }                bowee.bow(bower);            }        }    }    public static void main(String[] args) {        final Friend alphonse = new Friend("Alphonse");        final Friend gaston = new Friend("Gaston");        new Thread(new BowLoop(alphonse, gaston)).start();        new Thread(new BowLoop(gaston, alphonse)).start();    }}

执行器(Executors)

在之前所有的例子中,Thread对象表示的线程和Runnable对象表示的线程所执行的任务之间是紧耦合的。这对于小型应用程序来说没问题,但对于大规模并发应用来说,合理的做法是将线程的创建与管理和程序的其他部分分离开。封装这些功能的对象就是执行器,接下来的部分将讲详细描述执行器。 

  • 执行器接口定义了三种类型的执行器对象。
  • 线程池是最常见的一种执行器的实现。
  • Fork/Join是JDK 7中引入的并发框架。

 Executor接口

java.util.concurrent中包括三个Executor接口:

  • Executor,一个运行新任务的简单接口。
  • ExecutorService,扩展了Executor接口。添加了一些用来管理执行器生命周期和任务生命周期的方法。
  • ScheduledExecutorService,扩展了ExecutorService。支持Future和定期执行任务。

通常来说,指向Executor对象的变量应被声明为以上三种接口之一,而不是具体的实现类。

Executor接口

Executor接口只有一个execute方法,用来替代通常创建(启动)线程的方法。例如:r是一个Runnable对象,e是一个Executor对象。可以使用

e.execute(r);
来代替
(new Thread(r)).start();
但execute方法没有定义具体的实现方式。对于不同的Executor实现,execute方法可能是创建一个新线程并立即启动,但更有可能是使用已有的工作线程运行r,或者将r放入到队列中等待可用的工作线程。(我们将在线程池一节中描述工作线程。)

ExecutorService接口

ExecutorService接口在提供了execute方法的同时,新加了更加通用的submit方法。submit方法除了和execute方法一样可以接受Runnable对象作为参数,还可以接受Callable对象作为参数。使用Callable对象可以使任务返回执行的结果。通过submit方法返回的Future对象可以读取Callable任务的执行结果,或是管理Callable任务和Runnable任务的状态。 ExecutorService也提供了批量运行Callable任务的方法。最后,ExecutorService还提供了一些关闭执行器的方法。如果需要支持即时关闭,执行器所执行的任务需要正确处理中断。

ScheduledExecutorService接口

ScheduledExecutorService扩展ExecutorService接口并添加了schedule方法。调用schedule方法可以在指定的延时后执行一个Runnable或者Callable任务。ScheduledExecutorService接口还定义了按照指定时间间隔定期执行任务的scheduleAtFixedRate方法和scheduleWithFixedDelay方法。

线程池

在java.util.concurrent包中多数的执行器实现都使用了由工作线程组成的线程池,工作线程独立于所它所执行的Runnable任务和Callable任务,并且常用来执行多个任务。 使用工作线程可以使创建线程的开销最小化。在大规模并发应用中,创建大量的Thread对象会占用占用大量系统内存,分配和回收这些对象会产生很大的开销。 一种最常见的线程池是固定大小的线程池。这种线程池始终有一定数量的线程在运行,如果一个线程由于某种原因终止运行了,线程池会自动创建一个新的线程来代替它。需要执行的任务通过一个内部队列提交给线程,当没有更多的工作线程可以用来执行任务时,队列保存额外的任务。 使用固定大小的线程池一个很重要的好处是可以实现优雅退化。例如一个Web服务器,每一个HTTP请求都是由一个单独的线程来处理的,如果为每一个HTTP都创建一个新线程,那么当系统的开销超出其能力时,会突然地对所有请求都停止响应。如果限制Web服务器可以创建的线程数量,那么它就不必立即处理所有收到的请求,而是在有能力处理请求时才处理。 创建一个使用线程池的执行器最简单的方法是调用java.util.concurrent.ExecutorsnewFixedThreadPool方法。Executors类还提供了下列一下方法:

  • newCachedThreadPool方法创建了一个可扩展的线程池。适合用来启动很多短任务的应用程序。
  • newSingleThreadExecutor方法创建了每次执行一个任务的执行器。
  • 还有一些创建ScheduledExecutorService执行器的方法。

如果上面的方法都不满足需要,可以尝试java.util.concurrent.ThreadPoolExecutor或者java.util.concurrent.ScheduledThreadPoolExecutor。

Fork/Join

fork/join框架是ExecutorService接口的一种具体实现,目的是为了帮助你更好地利用多处理器带来的好处。它是为那些能够被递归地拆解成子任务的工作类型量身设计的。其目的在于能够使用所有可用的运算能力来提升你的应用的性能。 类似于ExecutorService接口的其他实现,fork/join框架会将任务分发给线程池中的工作线程。fork/join框架的独特之处在与它使用工作窃取(work-stealing)算法。完成自己的工作而处于空闲的工作线程能够从其他仍然处于忙碌(busy)状态的工作线程处窃取等待执行的任务。 fork/join框架的核心是ForkJoinPool类,它是对AbstractExecutorService类的扩展。ForkJoinPool实现了工作窃取算法,并可以执行ForkJoinTask任务。

基本使用方法

使用fork/join框架的第一步是编写执行一部分工作的代码。你的代码结构看起来应该与下面所示的伪代码类似:

if (当前这个任务工作量足够小)    直接完成这个任务else    将这个任务或这部分工作分解成两个部分    分别触发(invoke)这两个子任务的执行,并等待结果
你需要将这段代码包裹在一个ForkJoinTask的子类中。不过,通常情况下会使用一种更为具体的的类型,或者是RecursiveTask(会返回一个结果),或者是RecursiveAction。 当你的ForkJoinTask子类准备好了,创建一个代表所有需要完成工作的对象,然后将其作为参数传递给一个ForkJoinPool实例的invoke()方法即可。

要清晰,先模糊

想要了解fork/join框架的基本工作原理,接下来的这个例子会有所帮助。假设你想要模糊一张图片。原始的source图片由一个整数的数组表示,每个整数表示一个像素点的颜色数值。与source图片相同,模糊之后的destination图片也由一个整数数组表示。 对图片的模糊操作是通过对source数组中的每一个像素点进行处理完成的。处理的过程是这样的:将每个像素点的色值取出,与周围像素的色值(红、黄、蓝三个组成部分)放在一起取平均值,得到的结果被放入destination数组。因为一张图片会由一个很大的数组来表示,这个流程会花费一段较长的时间。如果使用fork/join框架来实现这个模糊算法,你就能够借助多处理器系统的并行处理能力。下面是上述算法结合fork/join框架的一种简单实现:

import java.awt.image.BufferedImage;import java.io.File;import java.util.concurrent.ForkJoinPool;import java.util.concurrent.RecursiveAction;import javax.imageio.ImageIO;/** * ForkBlur implements a simple horizontal image blur. It averages pixels in the * source array and writes them to a destination array. The sThreshold value * determines whether the blurring will be performed directly or split into two * tasks. * * This is not the recommended way to blur images; it is only intended to * illustrate the use of the Fork/Join framework. */// 可递归拆分成子任务的任务public class ForkBlur extends RecursiveAction {    private int[] mSource;    private int mStart;    private int mLength;    private int[] mDestination;    private int mBlurWidth = 15; // Processing window size, should be odd.    public ForkBlur(int[] src, int start, int length, int[] dst) {        mSource = src;        mStart = start;        mLength = length;        mDestination = dst;    }    // 将每个像素取出,与周围像素值一起取平均值,把结果写到destination    protected void computeDirectly() {        int sidePixels = (mBlurWidth - 1) / 2;        for (int index = mStart; index < mStart + mLength; index++) {            // Calculate average.            float rt = 0, gt = 0, bt = 0;            for (int mi = -sidePixels; mi <= sidePixels; mi++) {                int mindex = Math.min(Math.max(mi + index, 0), mSource.length - 1);                int pixel = mSource[mindex];                rt += (float) ((pixel & 0x00ff0000) >> 16) / mBlurWidth;                gt += (float) ((pixel & 0x0000ff00) >> 8) / mBlurWidth;                bt += (float) ((pixel & 0x000000ff) >> 0) / mBlurWidth;            }            // Re-assemble destination pixel.            int dpixel = (0xff000000)                    | (((int) rt) << 16)                    | (((int) gt) << 8)                    | (((int) bt) << 0);            mDestination[index] = dpixel;        }    }    //是否拆分成子任务的数组大小阀值    protected static int sThreshold = 10000;    @Override    protected void compute() {        if (mLength < sThreshold) {  //不拆分,直接完成任务            computeDirectly();            return;        }        int split = mLength / 2;  //拆分成两个小任务并行执行        //执行各个子任务        invokeAll(new ForkBlur(mSource, mStart, split, mDestination),                new ForkBlur(mSource, mStart + split, mLength - split,                 mDestination));    }    // Plumbing follows.    public static void main(String[] args) throws Exception {        //获取图片        String srcName = "red-tulips.jpg";        File srcFile = new File(srcName);        BufferedImage image = ImageIO.read(srcFile);                System.out.println("Source image: " + srcName);        //执行模糊处理        BufferedImage blurredImage = blur(image);        //写入处理后的图片        String dstName = "blurred-tulips.jpg";        File dstFile = new File(dstName);        ImageIO.write(blurredImage, "jpg", dstFile);                System.out.println("Output image: " + dstName);            }    public static BufferedImage blur(BufferedImage srcImage) {        int w = srcImage.getWidth();        int h = srcImage.getHeight();        //获取图片像素数组        int[] src = srcImage.getRGB(0, 0, w, h, null, 0, w);        int[] dst = new int[src.length];        System.out.println("Array size is " + src.length);        System.out.println("Threshold is " + sThreshold);        int processors = Runtime.getRuntime().availableProcessors();        System.out.println(Integer.toString(processors) + " processor"                + (processors != 1 ? "s are " : " is ")                + "available");        //创建要执行的任务        ForkBlur fb = new ForkBlur(src, 0, src.length, dst);        //创建并发执行器        ForkJoinPool pool = new ForkJoinPool();        long startTime = System.currentTimeMillis();        pool.invoke(fb);  //执行整个任务        long endTime = System.currentTimeMillis();        System.out.println("Image blur took " + (endTime - startTime) +                 " milliseconds.");        //保存处理后的图片        BufferedImage dstImage =                new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);        dstImage.setRGB(0, 0, w, h, dst, 0, w);        return dstImage;    }}
把要执行的任务封装在ForkBlur类中,它扩展了RecursiveAction,表示这是一个能够被递归地拆解成子任务的工作类型。接下来你需要实现父类中的compute()方法,它会直接执行模糊处理,或者将当前的工作拆分成两个更小的任务。数组的长度可以作为一个简单的阀值来判断任务是应该直接完成还是应该被拆分。拆分成小任务时,用invokeAll()执行各个小任务。

如果前面这个方法是在一个RecursiveAction的子类中,那么设置任务在ForkJoinPool中执行就再直观不过了(参考blue()中的代码)。通常会包含以下一些步骤:

  1. 创建一个表示所有需要完成工作的任务。
    // source image pixels are in src// destination image pixels are in dstForkBlur fb = new ForkBlur(src, 0, src.length, dst);

  2. 创建将要用来执行任务的ForkJoinPool
    ForkJoinPool pool = new ForkJoinPool();

  3. 执行任务。
    pool.invoke(fb);

在上面完整的源代码中,还包含一些创建destination图片文件的额外代码。

标准实现

除了能够使用fork/join框架来实现能够在多处理系统中被并行执行的定制化算法(如前文中的ForkBlur.java例子),在Java SE中一些比较常用的功能点也已经使用fork/join框架来实现了。在Java SE 8中,java.util.Arrays类的一系列parallelSort()方法就使用了fork/join来实现。这些方法与sort()系列方法很类似,但是通过使用fork/join框架,借助了并发来完成相关工作。在多处理器系统中,对大数组的并行排序会比串行排序更快。这些方法究竟是如何运用fork/join框架并不在本教程的讨论范围内。想要了解更多的信息,请参见Java API文档。 其他采用了fork/join框架的方法还包括java.util.streams包中的一些方法,此包是作为Java SE 8发行版中Project Lambda的一部分。想要了解更多信息,请参见Lambda Expressions一节。

并发集合

java.util.concurrent包囊括了Java集合框架的一些附加类。它们也最容易按照集合类所提供的接口来进行分类:

  • BlockingQueue定义了一个先进先出的数据结构,当你尝试往满队列中添加元素,或者从空队列中获取元素时,将会阻塞或者超时。
  • ConcurrentMap是java.util.Map的子接口,定义了一些有用的原子操作。移除或者替换键值对的操作只有当key存在时才能进行,而新增操作只有当key不存在时。使这些操作原子化,可以避免同步。ConcurrentMap的标准实现是ConcurrentHashMap,它是HashMap的并发模式。
  • ConcurrentNavigableMap是ConcurrentMap的子接口,支持近似匹配。ConcurrentNavigableMap的标准实现是ConcurrentSkipListMap,它是TreeMap的并发模式。

所有这些集合,通过 在集合里新增对象和访问或移除对象的操作之间,定义一个happens-before的关系,来帮助程序员避免内存一致性错误。

原子变量

java.util.concurrent.atomic包定义了对单一变量进行原子操作的类。所有的类都提供了get和set方法,可以使用它们像读写volatile变量一样读写原子类。就是说,同一变量上的一个set操作对于任意后续的get操作存在happens-before关系。原子的compareAndSet方法也有内存一致性特点,就像应用到整型原子变量中的简单原子算法。  为了看看这个包如何使用,让我们返回到最初用于演示线程干扰的Counter类:

class Counter {    private int c = 0;    public void increment() {        c++;    }    public void decrement() {        c--;    }    public int value() {        return c;    }}
使用同步是一种使Counter类变得线程安全的方法,如SynchronizedCounter:
class SynchronizedCounter {    private int c = 0;    public synchronized void increment() {        c++;    }    public synchronized void decrement() {        c--;    }    public synchronized int value() {        return c;    }}

对于这个简单的类,同步是一种可接受的解决方案。但是对于更复杂的类,我们可能想要避免不必要同步所带来的活跃度影响。将int替换为AtomicInteger允许我们在不进行同步的情况下阻止线程干扰,如AtomicCounter

import java.util.concurrent.atomic.AtomicInteger;class AtomicCounter {    private AtomicInteger c = new AtomicInteger(0);    public void increment() {        c.incrementAndGet();    }    public void decrement() {        c.decrementAndGet();    }    public int value() {        return c.get();    }}

并发随机数

在JDK7中,java.util.concurrent包含了一个相当便利的类,ThreadLocalRandom,当应用程序期望在多个线程或ForkJoinTasks中使用随机数时。

对于并发访问,使用TheadLocalRandom代替Math.random()可以减少竞争,从而获得更好的性能。

你只需调用ThreadLocalRandom.current(), 然后调用它的其中一个方法去获取一个随机数即可。下面是一个例子:

int r = ThreadLocalRandom.current() .nextInt(4, 77);


For Further Reading


  • Concurrent Programming in Java: Design Principles and Pattern (2nd Edition) by Doug Lea. A comprehensive work by a leading expert, who's also the architect of the Java platform's concurrency framework.
  • Java Concurrency in Practice by Brian Goetz, Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, and Doug Lea. A practical guide designed to be accessible to the novice.
  • Effective Java Programming Language Guide (2nd Edition) by Joshua Bloch. Though this is a general programming guide, its chapter on threads contains essential "best practices" for concurrent programming.
  • Concurrency: State Models & Java Programs (2nd Edition), by Jeff Magee and Jeff Kramer. An introduction to concurrent programming through a combination of modeling and practical examples.
  • Java Concurrent Animated: Animations that show usage of concurrency features.


Answers to Questions and Exercises: Concurrency


Questions

  1. Question: Can you pass a Thread object to Executor.execute? Would such an invocation make sense? Why or why not?

    Answer: Thread implements the Runnable interface, so you can pass an instance of Thread toExecutor.execute.However it doesn't make sense to use Thread objects this way. If the object is directly instantiated fromThread, itsrun method doesn't do anything. You can define a subclass of Thread with a usefulrun method — but such a class would implement features that the executor would not use.

Exercises

  1. Exercise: Compile and run BadThreads.java:

    public class BadThreads {    static String message;    private static class CorrectorThread        extends Thread {        public void run() {            try {                sleep(1000);             } catch (InterruptedException e) {}            // Key statement 1:            message = "Mares do eat oats.";         }    }    public static void main(String args[])        throws InterruptedException {        (new CorrectorThread()).start();        message = "Mares do not eat oats.";        Thread.sleep(2000);        // Key statement 2:        System.out.println(message);    }}

    The application should print out "Mares do eat oats." Is it guaranteed to always do this? If not, why not? Would it help to change the parameters of the two invocations of Sleep? How would you guarantee that all changes tomessage will be visible to the main thread?

    Solution: The program will almost always print out "Mares do eat oats." However, this result is not guaranteed, because there is no happens-before relationship between "Key statement 1" and "Key statement 2". This is true even if "Key statement 1" actually executes before "Key statement 2" — remember, a happens-before relationship is about visibility, not sequence.

    There are two ways you can guarantee that all changes to message will be visible to the main thread:

    • In the main thread, retain a reference to the CorrectorThread instance. Theninvokejoin on that instance before referring tomessage
    • Encapsulate message in an object with synchronized methods. Never referencemessage except through those methods.

    Both of these techniques establish the necessary happens-before relationship, making changes tomessage visible.

    A third technique is to simply declare message as volatile. This guarantees that any write tomessage (as in "Key statement 1") will have a happens-before relationship with any subsequent reads of message (as in "Key statement 2"). But it does not guarantee that "Key statement 1" willliterally happen before "Key statement 2". They willprobably happen in sequence, but because of scheduling uncertainities and the unknown granularity of sleep, this is not guaranteed.

    Changing the arguments of the two sleep invocations does not help either, since this does nothing to guarantee a happens-before relationship.

  2. Exercise: Modify the producer-consumer example in Guarded Blocks to use a standard library class instead of the Drop class.

    Solution: The java.util.concurrent.BlockingQueue interface defines a get method that blocks if the queue is empty, and a put methods that blocks if the queue is full. These are effectively the same operations defined byDrop — except that Drop is not a queue! However, there's another way of looking at Drop: it's a queue with a capacity of zero. Since there's no room in the queue for any elements, everyget blocks until the correspondingtake and every take blocks until the corresponding get.There is an implementation of BlockingQueue with precisely this behavior:java.util.concurrent.SynchronousQueue.

    BlockingQueue is almost a drop-in replacement for Drop. The main problem inProducer is that with BlockingQueue, theput and get methods throwInterruptedException. This means that the existingtry must be moved up a level:

    import java.util.Random;import java.util.concurrent.BlockingQueue;public class Producer implements Runnable {    private BlockingQueue<String> drop;    public Producer(BlockingQueue<String> drop) {        this.drop = drop;    }    public void run() {        String importantInfo[] = {            "Mares eat oats",            "Does eat oats",            "Little lambs eat ivy",            "A kid will eat ivy too"        };        Random random = new Random();        try {            for (int i = 0;                 i < importantInfo.length;                 i++) {                drop.put(importantInfo[i]);                Thread.sleep(random.nextInt(5000));            }            drop.put("DONE");        } catch (InterruptedException e) {}    }}
    Similar changes are required for Consumer:

    import java.util.Random;import java.util.concurrent.BlockingQueue;public class Consumer implements Runnable {    private BlockingQueue<String> drop;    public Consumer(BlockingQueue<String> drop) {        this.drop = drop;    }    public void run() {        Random random = new Random();        try {            for (String message = drop.take();                 ! message.equals("DONE");                 message = drop.take()) {                System.out.format("MESSAGE RECEIVED: %s%n",                                  message);                Thread.sleep(random.nextInt(5000));            }        } catch (InterruptedException e) {}    }}
    For ProducerConsumerExample, we simply change the declaration for the drop object:

    import java.util.concurrent.BlockingQueue;import java.util.concurrent.SynchronousQueue;public class ProducerConsumerExample {    public static void main(String[] args) {        BlockingQueue<String> drop =            new SynchronousQueue<String> ();        (new Thread(new Producer(drop))).start();        (new Thread(new Consumer(drop))).start();    }}


英文原文:http://docs.oracle.com/javase/tutorial/essential/concurrency/index.html

中文参考:http://ifeve.com/oracle-java-concurrency-tutorial/
0 0