多线程查找

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));
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值