• 多线程查找


    1.本算法采用的是将多线程查找实现方法:将大批量数据切割分成多份,让线程去寻找,如果找到了,则返回数据

    DavideSearch
    package cn.lonecloud.search;
    
    import java.util.LinkedList;
    import java.util.List;
    import java.util.concurrent.*;
    import java.util.concurrent.atomic.AtomicInteger;
    
    /**
     * @author lonecloud
     * @version v1.0
     * @date 下午3:37 2018/5/9
     */
    public class DavideSearch<T> {
    
        private T[] data;
    
        private int divCount;
    
        private AtomicInteger index;
    
        public DavideSearch(T[] data, int divCount) {
            this.data = data;
            this.divCount = divCount;
            index = new AtomicInteger(-1);
        }
    
        static ExecutorService service = new ThreadPoolExecutor(0, 10, 60, TimeUnit.SECONDS, new SynchronousQueue<>(), new ThreadFactory() {
            AtomicInteger integer = new AtomicInteger();
    
            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, "search-thread" + integer);
            }
        });
    
        public <T> int divSearch(T searchValue) {
            int subSize = data.length / divCount;
            for (int i = 0; i < data.length; i += subSize) {
                int end = i + subSize;
                if (end > data.length) {
                    end = data.length;
                }
                SearchTask<T> searchTask = new SearchTask<>(i, end, searchValue, (T[]) data, index);
                Future<Integer> submit = service.submit(searchTask);
            }
            return index.get();
        }
    }
    

     SearchTask

    package cn.lonecloud.search;
    
    import java.util.Objects;
    import java.util.concurrent.Callable;
    import java.util.concurrent.atomic.AtomicInteger;
    
    /**
     * @author lonecloud
     * @version v1.0
     * @date 上午9:52 2018/5/9
     */
    public class SearchTask<T> implements Callable<Integer> {
        private int begin, end;
        private T searchValue;
        private T[] data;
        private AtomicInteger result;
    
        public SearchTask(int begin, int end, T searchValue, T[] data, AtomicInteger index) {
            this.begin = begin;
            this.end = end;
            this.searchValue = searchValue;
            this.data = data;
            this.result = index;
        }
    
        @Override
        public Integer call() throws Exception {
            return search(searchValue, begin, end);
        }
    
        private int search(T searchValue, int begin, int end) {
            int i = 0;
            for (i = begin; i < end; i++) {
                if (data[i].equals(searchValue)) {
                    //并发如果其他线程先找到
                    if (!result.compareAndSet(-1, i)) {
                        return result.get();
                    }
                    return i;
                }
            }
            return -1;
        }
    }
    

    测试类

    package cn.lonecloud;
    
    import cn.lonecloud.search.DavideSearch;
    import org.junit.jupiter.api.Test;
    
    import java.util.Random;
    import java.util.UUID;
    
    /**
     * @author lonecloud
     * @version v1.0
     * @date 下午3:52 2018/5/9
     */
    public class MainTest {
    
        static final int MAX=100000;
        @Test
        public void integerTest() {
            Integer[] arr = new Integer[MAX];
            Random random = new Random();
            int index = random.nextInt(MAX);
            for (int i = 0; i < MAX; i++) {
                arr[i] = random.nextInt(MAX);
            }
            DavideSearch<Integer> davideSearch = new DavideSearch<>(arr, 3);
            int i = davideSearch.divSearch(arr[index]);
            System.out.print(index + "-------->" + i);
        }
    
        @Test
        public void StringTest() {
            long start = System.currentTimeMillis();
            String[] arr = new String[MAX];
            Random random = new Random();
            int index = random.nextInt(MAX);
            for (int i = 0; i < MAX; i++) {
                arr[i] = UUID.randomUUID().toString();
            }
            DavideSearch<String> davideSearch = new DavideSearch<>(arr, 3);
            int i = davideSearch.divSearch(arr[index]);
            System.out.println(index + "-------->" + i);
            System.out.println("end"+(System.currentTimeMillis()-start));
        }
        @Test
        public void NoramlSearch() {
            long start = System.currentTimeMillis();
            String[] arr = new String[MAX];
            Random random = new Random();
            int index = random.nextInt(MAX);
            for (int i = 0; i < MAX; i++) {
                arr[i] = UUID.randomUUID().toString();
            }
            for (int i = 0; i < arr.length; i++) {
                if (arr[i].equals(arr[index])){
                    System.out.println(index + "-------->" + i);
                }
            }
            System.out.println("end"+(System.currentTimeMillis()-start));
        }
    }
    
  • 相关阅读:
    在Eclipse/STS里添加代码反编译器(.class)步骤
    关于页面添加字段
    关于jeesite Date和时间戳问题
    关于前端ajax通过实体类向后端传参报不存在问题
    引入字典
    关于添加主键
    简单的前后端分离项目 部署到 centos7
    Cenost7 Mysql5.7 安装 并打开远程访问
    npm install vue-cli -g 报错
    docker 笔记 docker 基础 docker 常用命令
  • 原文地址:https://www.cnblogs.com/lonecloud/p/9014965.html
Copyright © 2020-2023  润新知