RecursiveAction 用法

package ForkJoinTest;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;

public class ListFileAction extends RecursiveAction {

    private File path;
    
    public ListFileAction(File path){
        this.path=path;
    }
    
    @Override
    protected void compute() {
        List<ListFileAction> subActions= new ArrayList<>();
        File[] files = path.listFiles();
        if(files !=null){
            for(File file : files){
                if(file.isDirectory()){
                    // 把遍历到是目录的文件添加到任务集合中
                    subActions.add(new ListFileAction(file));
                } else {
                    // 如果是文件,检查是否是TXT,是就打印
                    if(file.getAbsolutePath().endsWith("txt")){
                        System.out.println("文件:"+file.getAbsolutePath());
                    }
                }
            }
            //遍历完成后判断集合是否为空,不为空的话一起合并
            if(!subActions.isEmpty()){
                Collection<ListFileAction> endFils = invokeAll(subActions);
                
                for(ListFileAction subAction: endFils){
                    subAction.join();
                }
            }
        }
    }
    
    public static void main(String[] args) throws InterruptedException {
        // 声明一个forkjoin池调度任务
        ForkJoinPool pool = new ForkJoinPool();
        // 实例化类并给与其从哪里开始执行的文件路径
        ListFileAction action = new ListFileAction(new File("F:/"));
        // 异步提交
        pool.execute(action);
        System.out.println("Task is Running.....");
        Thread.sleep(500);
        int otherWork=0;
        for(int i = 0 ; i < 100;i++){
            otherWork=otherWork+i;
        }
        
        System.out.println("Main Thread done sth..... , otherWork="+otherWork);
        //阻塞方法
        action.join();
        System.out.println("Task end");
    }
 
}
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值