Java7新特性(四)并发 8 forkjoin分支合并框架

本文主要根据《Java程序员修炼之道》整理的代码笔记片段

采用“工作窃取”的算法解决大小不同任务所导致的调度问题

场景:

模拟大量简单对象的运动

日志文件分析

从输入中计数的数操作,如mapreduce   java jdk排序算法SortTask采用该框架

RecursiveAction 继承ForkJoinTask<Void>

public class MicroBlogUpdateSorter extends RecursiveAction {


	private static final long serialVersionUID = 2077227050172847747L;


	private static final int SMALL_ENOUGH = 32;
	private final Update[] updates;
	private final int start, end;
	private final Update[] result;


	public MicroBlogUpdateSorter(Update[] updates_) {
		this(updates_, 0, updates_.length);
	}


	public MicroBlogUpdateSorter(Update[] upds_, int startPos_, int endPos_) {
		start = startPos_;
		end = endPos_;
		updates = upds_;
		result = new Update[updates.length];
	}


	private void merge(MicroBlogUpdateSorter left_, MicroBlogUpdateSorter right_) {
		int i = 0;
		int lCt = 0;
		int rCt = 0;
		while (lCt < left_.size() && rCt < right_.size()) {
			result[i++] = (left_.result[lCt].compareTo(right_.result[rCt]) < 0) ? left_.result[lCt++]
					: right_.result[rCt++];
		}
		while (lCt < left_.size())
			result[i++] = left_.result[lCt++];
		while (rCt < right_.size())
			result[i++] = right_.result[rCt++];
	}


	public int size() {
		return end - start;
	}


	public Update[] getResult() {
		return result;
	}


	@Override
	protected void compute() {
		if (size() < SMALL_ENOUGH) {
			System.arraycopy(updates, start, result, 0, size());
			Arrays.sort(result, 0, size());
		} else {
			int mid = size() / 2;
			MicroBlogUpdateSorter left = new MicroBlogUpdateSorter(updates, start, start + mid);
			MicroBlogUpdateSorter right = new MicroBlogUpdateSorter(updates, start + mid, end);
			invokeAll(left, right);
			merge(left, right);
		}
	}
}
public class UpdateSorterMain {

  public static void main(String[] args) {
    List<Update> lu = new ArrayList<Update>();
    String text = "";
    final Update.Builder ub = new Update.Builder();
    final Author a = new Author("Tallulah");

    for (int i = 0; i < 256; i++) {
      text = text + "X";
      long now = System.currentTimeMillis();
      lu.add(ub.author(a).updateText(text).createTime(now).build());
      try {
        Thread.sleep(1);
      } catch (InterruptedException e) {
      }
    }
    Collections.shuffle(lu);
    Update[] updates = lu.toArray(new Update[0]); // Avoid allocation by passing
                                                  // zero-sized array
    MicroBlogUpdateSorter sorter = new MicroBlogUpdateSorter(updates);
    ForkJoinPool pool = new ForkJoinPool(4);
    pool.invoke(sorter);

    for (Update u : sorter.getResult()) {
      System.out.println(u);
    }
  }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值