多线程查找
1.本算法采用的是将多线程查找实现方法:将大批量数据切割分成多份,让线程去寻找,如果找到了,则返回数据
1 | DavideSearch |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | 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 ; } } |
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | 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)); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了