Java 多线程处理集合数据

处理一个包含10万条数据的字符串集合,将其中含有'4599'的字符串都返回。

1.开启线程代码如下:

import com.baidu.model.ConCallable; 
import com.baidu.model.PropertiesTest; 
import com.google.common.collect.ImmutableList; 
import com.google.common.util.concurrent。*; 
导入org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RestController; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.concurrent.CountDownLatch; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
import java.util.concurrent.TimeUnit; 

/ **多线程,线程池使用类
 *由Mypc于2018/3/21 0021创建。
 * / 
@RestController 
@RequestMapping(“thread”)
public class ThreadController { 

    private Logger logger = LoggerFactory.getLogger(org.slf4j.Logger.class); 

    @RequestMapping(value =“test1”,method = RequestMethod.GET)
    public String test1(){ 
        ExecutorService executorService = null; 
        try { 
            // 10万条数据
            List <String> list = new ArrayList <>(); 
            List <String> list2 = new ArrayList <>(); 

            for(int i = 1; i <= 100000; i ++){ 
                list.add(“test:”+ i);




            int count = list.size()/ size; 
            if(count * size!= list.size()){ 
                count ++; 
            } 
            int countNum = 0; 
            //线程计数器
            final CountDownLatch countDownLatch = new CountDownLatch(count); 
            //初始化一个定长线程池对象
            executorService = Executors.newFixedThreadPool(8); 
            //监听线程池
            ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(executorService); 
            while(countNum <list.size()){ 
                countNum + = size; 
                //创建线程任务
                ConCallable callable = new ConCallable();
                //分列400份)的数据,分给不同线程处理
                callable.setList(ImmutableList.copyOf(list.subList(countNum - size,countNum <list.size()?countNum:list.size())) ); 
                ListenableFuture listenableFuture = listeningExecutorService.submit(callable); 
                Futures.addCallback(listenableFuture,new FutureCallback <List <String >>(){ 
                    @Override 
                    public void onSuccess(List <String> list1){ 
                        countDownLatch.countDown(); 
                        // list1为返回值
                        list2.addAll(list1); 
                    } 

                    @Override 
                    public void onFailure(Throwable throwable){
                        countDownLatch.countDown(); 
                        logger.info( “处理出错:”,抛出); 
                    } 
                }); 
            } 
            countDownLatch.await(30,TimeUnit.MINUTES); 
            logger.info( “符合条件的返回数据个数为:” + list2.size()); 
            logger.info( “回调函数:” + list2.toString()); 
        catch(Exception e){ 
            e.printStackTrace(); 
        } finally { 
            //关闭资源
            executorService.shutdown(); 
        } 
        return“正在处理......”;

    }

2.线程任务类代码:

/ ** 
 *由Mypc于2018/3/21 0021创建。
 * / 
public class ConCallable implements Callable { 

    private List <String> list; 

    @Override 
    public Object call()抛出异常{ 
        List listRe = new ArrayList(); 
        for(int i = 0; i <list.size(); i ++){ 
            //含有'4599'的字符串都返回
            if(list.get(i).contains(“4599”)){ 
                listRe.add list.get(I)); 
            } 
        } 
        return listRe; 
    } 

    public List <String> getList(){ 
        return list; 
    } 

    public void setList(List <String> list){ 
        this.list = list; 
    } 
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值