一个并行搜索算法

抄自《实战Java高并发程序设计》

package understanding;

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 ParallelSearch {

	//要找的数组
	static int[] arr = {1,2,4,5,6,3,4,5,6,7,8};
	
	//cachedThreadPool,按需创建线程池,有就用,没有就创建,过了一段时间还空闲就撤销线程
	//Creates a thread pool that creates new threads as needed, 
	//but will reuse previously constructed threads when they are available. 
	//These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks. 
	//Calls to execute will reuse previously constructed threads if available. 
	//If no existing thread is available, a new thread will be created and added to the pool. 
	//Threads that have not been used for sixty seconds are terminated and removed from the cache. 
	//Thus, a pool that remains idle for long enough will not consume any resources. 
	//Note that pools with similar properties but different details (for example, timeout parameters) 
	//may be created using ThreadPoolExecutor constructors.
	static ExecutorService pool = Executors.newCachedThreadPool();
	static final int threadNum = 2;
	
	//存放下标,表示要找的值在这个位置
	static AtomicInteger result = new AtomicInteger(-1);
	
	static class SearchTask implements Callable<Integer>{

		//分别表示开始下标,结束下标,要搜索的值
		int begin,end,searchValue;
		
		//构造方法
		SearchTask(int b,int e,int searchValue){
			this.begin = b;
			this.end = e;
			this.searchValue = searchValue;
		}
		
		@Override
		public Integer call() throws Exception {
			return search(begin, end, searchValue);
		}
		
	}

	//搜索任务要调用的搜索方法,这个方法是关键。
	public static int search(int b,int e,int val){
		
		for(int i=b;i<e;i++){
			if(result.get()>=0){	///已经找到结果了,无需再找,直接返回
				return result.get();
			}
			//没有找到,找找看
			if(arr[i]==val){	//找到了,利用CAS操作设置result的值
				if(!result.compareAndSet(-1, i)){  //设置失败,表示原来的值已经不是-1,说明已经找到了一个结果
					return result.get();
				}
				return i;	//设置成功,返回i
			}
		}
		
		return -1;
	}
	
	public static int pSearch(int searchValue) throws InterruptedException, ExecutionException{
		int subArrSize = arr.length/threadNum+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 SearchTask(i, end, searchValue)));
		}
		for(Future<Integer> futrue:re){
			if(futrue.get()>=0){
				return futrue.get();
			}
		}
		return -1;
	}
	
	public static void main(String[] args) {
		try {
			int res = pSearch(3);
			System.out.println(res);
		} catch (InterruptedException | ExecutionException e) {
			e.printStackTrace();
		}
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值