并发编程之移相器Phaser

示例程序:

1、Horse程序

import java.util.concurrent.atomic.AtomicInteger;

public class Horse implements Runnable {
	private final static AtomicInteger idSource = new AtomicInteger();
	private final int id = idSource.incrementAndGet();
	@Override
	public void run() {
		System.out.println(toString() + ": running");
	}
	
	@Override
	public String toString() {
		return "horse #" + id;
	}
}
2、HorseRace
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.Phaser;

public class HorseRace {
	private final int NUMBER_OF_HORSES = 12;
	private final static int INIT_PARTIES = 1;
	public final static Phaser manager = new Phaser(INIT_PARTIES);

	public static void main(String[] args) {
		Thread raceMonitor = new Thread(new RaceMonitor());
		raceMonitor.setDaemon(true);
		raceMonitor.start();
		new HorseRace().manageRace();
	}

	private void manageRace() {
		ArrayList<Horse> horseArray = new ArrayList<Horse>();
		for (int i = 0; i < NUMBER_OF_HORSES; i++) {
			horseArray.add(new Horse());
		}
		runRace(horseArray);
	}

	private void runRace(Iterable<Horse> team) {
		System.out.println("Assign all horses, then start race");
		for (final Horse horse : team) {
			final String dev = horse.toString();
			System.out.println("assign " + dev + " to the race");
			manager.register(); //将马匹注册到移相器中
			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						Thread.sleep((new Random()).nextInt(1000));
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println(dev + ", please await all horses");
					manager.arriveAndAwaitAdvance();
					horse.run();
				}
			}).start();
		}
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("All arrived at starting gate, start race");
		manager.arriveAndDeregister();
	}
}
3、监视器

public class RaceMonitor implements Runnable {
	@Override
	public void run() {
		while (true) {
			System.out.println("Number of horses ready to run: "
					+ HorseRace.manager.getArrivedParties());
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
4、运行结果:

Number of horses ready to run: 0
.................................0
Number of horses ready to run: 0
assign horse #2 to the race
assign horse #3 to the race
assign horse #4 to the race
assign horse #5 to the race
assign horse #6 to the race
Number of horses ready to run: 0
assign horse #7 to the race
assign horse #8 to the race
assign horse #9 to the race
assign horse #10 to the race
assign horse #11 to the race
assign horse #12 to the race
Number of horses ready to run: 0
..............................0
Number of horses ready to run: 0
horse #12, please await all horses
Number of horses ready to run: 1
.............................1
Number of horses ready to run: 1
horse #1, please await all horses
Number of horses ready to run: 2
...............................2
Number of horses ready to run: 2
horse #3, please await all horses
Number of horses ready to run: 3
.............................. 3
Number of horses ready to run: 3
Number of horses ready to run: 3
horse #8, please await all horses
Number of horses ready to run: 4
.............................4
Number of horses ready to run: 4
horse #4, please await all horses
Number of horses ready to run: 5
Number of horses ready to run: 5
................................5
Number of horses ready to run: 5
horse #9, please await all horses
Number of horses ready to run: 6
Number of horses ready to run: 6
Number of horses ready to run: 6
...........................6
Number of horses ready to run: 6
horse #10, please await all horses
Number of horses ready to run: 6
Number of horses ready to run: 7
..............................7
Number of horses ready to run: 7
horse #6, please await all horses
Number of horses ready to run: 8
................................8
Number of horses ready to run: 8
horse #5, please await all horses
Number of horses ready to run: 9
Number of horses ready to run: 9
................................9
Number of horses ready to run: 9
horse #2, please await all horses
Number of horses ready to run: 10
..............................10
Number of horses ready to run: 10
horse #11, please await all horses
Number of horses ready to run: 11
................................11
Number of horses ready to run: 11
horse #7, please await all horses
Number of horses ready to run: 12
................................12
Number of horses ready to run: 12
Number of horses ready to run: 12
All arrived at starting gate, start race
horse #7: running
horse #11: running
horse #4: running
horse #8: running
horse #3: running
horse #12: running
Number of horses ready to run: 0
horse #2: running
horse #5: running
horse #6: running
Number of horses ready to run: 0
horse #10: running
horse #9: running
horse #1: running




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值