Java编程思想-java中的并发(四)

来源:互联网 发布:arm linux gcc 编辑:程序博客网 时间:2024/05/19 01:31

五、 新类库中的构件

Java SE5的java.util.concurrent引入了大量设计用来解决并发问题的新类。学习使用它们将有助于编写出更加简单而强壮的并发程序。

1. CountDownLatch

他被用来同步一个或多个任务,强制他们等待由其他任务执行的一组操作完成。

你可以向CountDownLatch对象设置一个初始计数值,任何在这个对象上调用wait()方法都将阻塞,直至这个计数值到达0。其他任务在结束其工作时,可以在该对象上盗用countDown()来减小这个计数值。CountDownLatch被设计为只触发一次,计数值不能被重置。如果你需要能够重置计数值的版本,则可以使用CyclicBarrier。

调用countDown()的任务在产生这个调用时并没有被阻塞,只有对await()的调用会被阻塞,直至计数值到达0。

CountDownLatch的典型用法是将一个程序氛围n个相互独立的可解决任务,并创建值为0的CountDownLatch。当每个任务完成时,都会在这个锁存器上调用countDown()。等待问题呗解决的任务在这个锁存器上调用await(),将他们自己拦住,直至锁存器计数结束。下面是演示这种技术的一个框架示例:

public class CountDownLatchDemo {    static final int SIZE = 100;    public static void main(String[] args) throws Exception {        ExecutorService exec = Executors.newCachedThreadPool();        CountDownLatch latch = new CountDownLatch(SIZE);        for (int i = 0; i < 10; i++) {            exec.execute(new WaitingTask(latch));        }        for (int i = 0; i < SIZE; i++) {            exec.execute(new TaskPortion(latch));        }        System.out.println("Launched all tasks");        exec.shutdown();    }}class TaskPortion implements Runnable {    private static int counter = 0;    private final int id = counter++;    private static Random rand = new Random(47);    private final CountDownLatch latch;    TaskPortion(CountDownLatch latch) {        this.latch = latch;    }    public void run() {        try {            doWork();            latch.countDown();        } catch (InterruptedException e) {            //Acceptable way to exit        }    }    public void doWork() throws InterruptedException {        TimeUnit.MILLISECONDS.sleep(rand.nextInt(2000));        System.out.println(this + "completed");    }    public String toString() {        return String.format("%1$-3d", id);    }}class WaitingTask implements Runnable {    private static int counter = 0;    private final int id = counter++;    private final CountDownLatch latch;    WaitingTask(CountDownLatch latch) {        this.latch = latch;    }    public void run() {        try {            latch.await();            System.out.println("Latch barrier passed for " + this);        } catch (InterruptedException e) {            System.out.println(this + " interrupted");        }    }    public String toString() {        return String.format("WaitingTask %1$-3d", id);    }}

TaskPortion将随机的休眠一段时间,以模拟这部分工作的完成,而WaitingTask表示系统中必须等待的部分,它要等待到问题的初始部分完成为止。所有任务都使用了在main()中定义的同一个单一的CountDownLatch。

2. CyclicBarrier

CyclicBarrier适用于这样的情况,你希望创建一组任务,他们并行的执行任务,然后在进行下一个步骤前等待,直至所有任务都完成(看起来有些像join())。它使得所有的并行任务都在栅栏处列队,因此可以一致的向前移动。这非常像CountDownLatch,只是CountDownLatch是只触发一次的事件,而CyclicBarrier可以多次重用。

public class HorseRace {    static final int FINISH_LINE = 75;    private List<Horse> horses = new ArrayList<Horse>();    private ExecutorService exec = Executors.newCachedThreadPool();    private CyclicBarrier barrier;    public HorseRace(int nHorses, final int pause) {        barrier = new CyclicBarrier(nHorses, () -> {            StringBuilder s = new StringBuilder();            for (int i = 0; i < FINISH_LINE; i++) {                s.append("=");            }            System.out.println(s);            for (Horse horse : horses) {                System.out.println(horse.tracks());            }            for (Horse horse : horses) {                if (horse.getStrides() >= FINISH_LINE) {                    System.out.println(horse + " won!");                    exec.shutdownNow();                    return;                }            }            try {                TimeUnit.MILLISECONDS.sleep(pause);            } catch (InterruptedException e) {                System.out.println("barrier-action sleep interrupted");            }        });        for (int i = 0; i < nHorses; i++) {            Horse horse = new Horse(barrier);            horses.add(horse);            exec.execute(horse);        }    }    public static void main(String[] args) {        int nHorses = 7;        int pause = 200;        if (args.length > 0) {            int n = new Integer(args[0]);            nHorses = n > 0 ? n : nHorses;        }        if (args.length > 1) {            int p = new Integer(args[1]);            pause = p > -1 ? p : pause;        }        new HorseRace(nHorses, pause);    }}class Horse implements Runnable {    private static int counter = 0;    private final int id = counter++;    private int strides = 0;    private static Random rand = new Random(47);    private static CyclicBarrier barrier;    public Horse(CyclicBarrier barrier) {        this.barrier = barrier;    }    public synchronized int getStrides() {        return strides;    }    public void run() {        try {            while (!Thread.interrupted()) {                synchronized (this) {                    strides += rand.nextInt(3);                }                barrier.await();            }        } catch (InterruptedException e) {            //a legitimate way to exit        } catch (BrokenBarrierException e) {            //This one we want to know about            throw new RuntimeException(e);        }    }    public String toString() {        return "Horse " + id + "  ";    }    public String tracks() {        StringBuilder s = new StringBuilder();        for (int i = 0; i < getStrides(); i++) {            s.append("*");        }        s.append(id);        return s.toString();    }}

可以向CyclicBarrier提供一个“栅栏动作”,他是一个Runnable,当计数值到达0时自动执行–这是CyclicBarrier和CountDownLatch之间的另一个区别。这里,栅栏动作是作为匿名内部类创建的,他被提交给了CyclicBarrier的构造器。

3. DelayQueue

这是一个无界的BlockingQueue,用于放置实现了Delayed接口的对象,其中的对象只能在其到期时才能从队列中取走。这种队列是有序的,即队头对象的延迟到期的时间最长。如果没有任何延迟到期,那么就不会y任何头元素,并且poll()将返回null(正因为这样,你不能将null放置到这种队列中)。

4. Semaphore

正常的锁在任何时刻都只允许yi’ge’ren’wu一个任务访问一项资源,而计数信号量允许n个任务同时访问这个资源。你还可以将信号量看作是在向外分发使用资源的“许可证”,尽管实际上没有使用任何许可证对象。

作为一个示例,请考虑对象池的概念,他管理着数量有限的对象,当要使用对象时可以签出他们,而在用户使用完毕时,可以将他们签回。这种功能可以被封装到一个泛型类中:

public class Pool<T> {    private int size;    private List<T> items = new ArrayList<T>();    private volatile boolean[] checkedOut;    private Semaphore available;    public Pool(Class<T> classObject, int size) {        this.size = size;        checkedOut = new boolean[size];        available = new Semaphore(size, true);        for (int i = 0; i < size; i++) {            try {                items.add(classObject.newInstance());            } catch (Exception e) {                throw new RuntimeException(e);            }        }    }    public T checkOut() throws InterruptedException {        available.acquire();        return getItem();    }    public void checkIn(T x) {        if (releaseItem(x)) {            available.release();        }    }    private synchronized T getItem() {        for (int i = 0; i < size; i++) {            if (!checkedOut[i]) {                checkedOut[i] = true;                return items.get(i);            }        }        return null;    }    private synchronized boolean releaseItem(T item) {        int index = items.indexOf(item);        if (index == -1) {            return false;        }        if (checkedOut[index]) {            checkedOut[index] = false;            return true;        }        return false;    }}

在这个简化的形式中,构造器使用newInstance()来把对象加载到池中。如果你需要一个新对象,那么可以调用checkOut(),并且在使用完之后,将其递交给checkIn()。

为了创建一个示例,我们可以使用Fat,这是一种创建代价高昂的对象类型,因为他的构造器运行起来很耗时:

public class Fat {    private volatile double d;    private static int counter = 0;    public Fat() {        for (int i = 1; i < 10000; i++) {            d += (Math.PI + Math.E) / (double) i;        }    }    public void operation() {        System.out.println(this);    }    public String toString() {        return "Fat id: " + d;    }}

我们在池中管理这些对象,以限制这个构造器所造成的影响。我们可以创建一个任务,它将签出Fat对象,持有一段时间后再将他们签入,以此来测试Pool这个类:

public class SemaphoreDemo {    final static int SIZE = 25;    public static void main(String[] args) throws Exception {        final Pool<Fat> pool = new Pool<>(Fat.class, SIZE);        ExecutorService exec = Executors.newCachedThreadPool();        for (int i = 0; i < SIZE; i++) {            exec.execute(new CheckoutTask<Fat>(pool));        }        System.out.println("All CheckoutTasks created");        List<Fat> list = new ArrayList<>();        for (int i = 0; i < SIZE; i++) {            Fat f = pool.checkOut();            System.out.println(i + ": main() thread checked out");            f.operation();            list.add(f);        }        Future<?> blocked = exec.submit(new Runnable() {            public void run() {                try {                    pool.checkOut();                } catch (InterruptedException e) {                    System.out.println("checkout() interrupted");                }            }        });        TimeUnit.SECONDS.sleep(2);        blocked.cancel(true);        System.out.println("Checking in objects in " + list);        for (Fat f : list) {            pool.checkIn(f);        }        for (Fat f : list) {            pool.checkIn(f);        }        exec.shutdown();    }}class CheckoutTask<T> implements Runnable {    private static int counter = 0;    private final int id = counter++;    private Pool<T> pool;    public CheckoutTask(Pool<T> pool) {        this.pool = pool;    }    public void run() {        try {            T item = pool.checkOut();            System.out.println(this + " checked out " + item);            TimeUnit.SECONDS.sleep(1);            System.out.println(this + " checked in " + item);            pool.checkIn(item);        } catch (InterruptedException e) {            //Acceptable way to terminate        }    }    public String toString() {        return "CheckoutTask " + id + "  ";    }}

在main()中,创建了一个持有Fat对象的Pool,而一组CheckoutTask则开始操练这个Pool。然后,main()线程签出池中的Fat对象,但是并不签入他们。一旦池中所有的对象都被签出,Semaphore将不再允许执行任何签出操作。blocked的run()方法因此会被阻塞,2秒之后,cancel()方法被调用,以此来挣脱Future的束缚。注意,冗余的qian’ru签入将被Pool忽略。

0 0
原创粉丝点击