码迷,mamicode.com
首页 > 编程语言 > 详细

多线程过滤敏感词

时间:2017-08-07 13:52:42      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:cut   call()   submit   https   mit   stat   nbsp   null   keyword   

原文:https://git.oschina.net/tianzhenjiu/codes/y9z6t471s3euinoj5vcdf79

 

package io.test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Testoss {

    public static void main(String[] args) throws Exception {    
        
        String[] keywords={"毛","周","二逼","xx","王五","刘能","你好吗","共 产 党","哈哈","中国人"};
        
        String content="你好哈222哈2哈啊大a我们是中国人在主实例的读请求较多、读压力比较大的时候,可以通过 DRDS 读写分离功能对读流量进行分流,减轻 RDS 主实例的读压力。你好哈222哈2哈啊大a我们是中国人在主实例的读请求较多、读压力比较大的时候,可以通过 DRDS 读写分离功能对读流量进行分流,减轻 RDS 主实例的读压力。你好哈222哈2哈啊大a我们是中国人在主实例的读请求较多、读压力比较大的时候,可以通过 DRDS 读写分离功能对读流量进行分流,减轻 RDS 主实例的读压力。你好哈222哈2哈啊大a我们是中国人在主实例的读请求较多、读压力比较大的时候,可以通过 DRDS 读写分离功能对读流量进行分流,减轻 RDS 主实例的读压力。你好哈222哈2哈啊大a我们是中国人在主实例的读请求较多、读压力比较大的时候,可以通过 DRDS 读写分离功能对读流量进行分流,减轻 RDS 主实例的读压力。你好哈222哈2哈啊大a我们是中国人在主实例的读请求较多、读压力比较大的时候,可以通过 DRDS 读写分离功能对读流量进行分流,减轻 RDS 主实例的读压力。";
        int splitthread=2;
        
        int count=keywords.length/splitthread;
        
        ExecutorService executorService=Executors.newFixedThreadPool(4);
        
        boolean filterit=false;
        
        
        
        List<Future<String>> futures=new ArrayList<>();
        for(int i=0;i<keywords.length;i+=count){
            
            final List<String> strings=new ArrayList<>();
            
            for(int j=0;i+j<keywords.length&&
                        j<count;j++){
        
                strings.add(keywords[i+j]);
            }
            
            futures.add(executorService.submit(new Callable<String>() {
                @Override
                public String call() throws Exception {
                
                    return filter(content, strings);
                }
                
                
            }));
            
            
        }
        
        for(Future<String> future:futures){
            
            if(future.get()!=null){
                filterit=true;
                System.out.println(future.get());
            }
        }
        
        executorService.shutdown();
        
        System.out.println(filterit);
        
        
    }
    
    /**
     * 
     * @param content  内容
     * @param keywords 过滤词组
     * @return
     */
    public static String filter(String content,List<String> keywords){
//        
//        try {
//            Thread.sleep(1000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        
        for(String keyword:keywords){
            
            if(content.indexOf(keyword)>0)
                return keyword;
         }
        
        return null;
    }
    

}

 

多线程过滤敏感词

标签:cut   call()   submit   https   mit   stat   nbsp   null   keyword   

原文地址:http://www.cnblogs.com/shihaiming/p/7298326.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!