對無序數組的並發搜尋的java實現

來源:互聯網
上載者:User

標籤:span   優勢   實現   except   通過   system   lis   executors   turn   

對無序數組的並發搜尋的實現可以充分的用到多cpu的優勢

一種簡單的策略是將原始數組按照期望的線程數進行分割,如果我們計劃使用兩個線程進行搜尋,就可以把一個數組分成兩個,每個線程各自獨立的搜尋,當其中有一個線程找到資料後,立即返回結果的index即可。

首先index需要採用atomicinteger來進行修飾,預設初始化的值為-1,意義為當前未找到,由於內部採用CAS機制,線程在遍曆比較是否相等之前,會通過atomicinteger中的get方法拿到當前的值,如果大於等於0,那麼說明別的線程已經找到了結果,直接返回get值就可以。如果比較的過程中發現相等了,那麼調用atomicinteger中的compareAndSet(-1,i),如果方法返回成功,則說明當前的線程是第一個發現結果的,那麼返回當前index即可,如果失敗,則說明別的線程先獲得了結果,直接返回atomicinteger中的get方法擷取的值即可。

整個過程採用future實現,拿到了Future後,不斷地輪詢結果,如果大於0即返回結果。

具體實現:

package parallel;import java.util.ArrayList;import java.util.List;import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;import java.util.concurrent.atomic.AtomicInteger;public class SeatchTask implements Callable<Integer>{    static int[] arr = {2,34,5,6};    static ExecutorService pool = Executors.newCachedThreadPool();    static final int Thread_Num = 2;    static AtomicInteger result = new AtomicInteger(-1);    int begin,end,searchValue;    public static int search(int searchValue, int beginPos, int endPos){        int i = 0;        for(i = beginPos; i < endPos;i ++){            if(result.get() > 0){                return result.get();            }            if(arr[i] == searchValue){                if(!result.compareAndSet(-1, i)){                    return result.get();                }                return i;            }        }        return -1;    }    @Override    public Integer call() throws Exception {        int re = search(searchValue, begin, end);        return re;    }    public SeatchTask(int searchValue, int begin, int end){        this.searchValue = searchValue;        this.begin = begin;        this.end = end;    }    public static int pSearch(int searchValue) throws InterruptedException, ExecutionException{        int subArrSize = arr.length/Thread_Num + 1;        List<Future<Integer>> re = new ArrayList<Future<Integer>>();        for(int i = 0;i < arr.length;i +=subArrSize){            int end = i + subArrSize;            if(end <= arr.length) end = arr.length;            re.add(pool.submit(new SeatchTask(searchValue, i, end)));        }        for(Future<Integer> fu : re){            if(fu.get() >= 0){                return fu.get();            }        }        return -1;    }    public static void main(String[] args) throws InterruptedException, ExecutionException {        int index = pSearch(34);        System.out.println(index);    }}

 

對無序數組的並發搜尋的java實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.