Package forkjoin

Source Code of forkjoin.DataLinkThread

package forkjoin;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.concurrent.RecursiveTask;

import cputils.CPHMWYService;

public class DataLinkThread extends RecursiveTask<List<String>>{
  private static final long serialVersionUID = -4046194613023076351L;

  private static final int THRESHOLD = 10;
    private int start;
    private int end;
   
    private static CPHMWYService wyService = new CPHMWYService();
   
    private Object target = null;

    public Object getTarget() {
    return target;
  }

  public void setTarget(Object target) {
    this.target = target;
  }

  public DataLinkThread(int start, int end) {
        this.start = start;
        this.end = end;
    }

  @Override
  protected List<String> compute() {
    List<String> alllist = new ArrayList<String>();
    List<String> list = null;;
        if((end - start) < THRESHOLD){
            for(int i = start; i<= end;i++){
              //list = wyService.readDetailLink163("2014043", 3, i);
              try {
          Method method = target.getClass().getDeclaredMethod("readDetailLink163", String.class,Integer.class,String.class,Integer.class);
          list = (List<String>) method.invoke(target, "2014043", 3, i);
        } catch (NoSuchMethodException e) {
          e.printStackTrace();
        } catch (SecurityException e) {
          e.printStackTrace();
        } catch (IllegalAccessException e) {
          e.printStackTrace();
        } catch (IllegalArgumentException e) {
          e.printStackTrace();
        } catch (InvocationTargetException e) {
          e.printStackTrace();
        }
             
              if (list.isEmpty()) {
          break;
        }
                alllist.addAll(list);
            }
           // System.out.println("["+Thread.currentThread().getName()+"]start:"+start+" end:"+end);
            return alllist;
        }
        int middle = (start + end) /2;
        DataLinkThread left = new DataLinkThread(start, middle);
        DataLinkThread right = new DataLinkThread(middle + 1, end);
        left.fork();
        right.fork();
        alllist.addAll(left.join());
        alllist.addAll(right.join());
        return alllist;
  }
 
 
  public static void main(String[] args) throws InterruptedException, ExecutionException {
        List<String> alllist = new ArrayList<String>();
        long b = System.currentTimeMillis();
    //alllist = wyService.wyDataLinks("2014043", 3);
    //System.out.println("顺序执行:size:"+alllist.size()+"time:"+(System.currentTimeMillis()-b));
    //for (int i = 0; i < 5; i++) {
      b = System.currentTimeMillis();
      ForkJoinPool forkJoinPool = new ForkJoinPool(Runtime.getRuntime().availableProcessors());
      int linkPages = wyService.dataLinkPages("2014043", 3);
      DataLinkThread thread = new DataLinkThread(1,linkPages);
      thread.setTarget(thread);
          Future<List<String>> result = forkJoinPool.submit(thread);
          forkJoinPool.shutdown();
         //System.out.println("ForkJoin:size:"+result.get().size()+" time:"+(System.currentTimeMillis()-b));
    //}
       
  }

}
TOP

Related Classes of forkjoin.DataLinkThread

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.