多執行緒查詢

lonecloud發表於2018-05-09

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

 

相關文章