forked from guanpengchn/java-concurrent-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
63 lines (53 loc) · 1.74 KB
/
Copy pathMain.java
File metadata and controls
63 lines (53 loc) · 1.74 KB
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
61
62
63
package ch5.s7;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
static int[] arr;
static ExecutorService pool = Executors.newCachedThreadPool();
static final int Thread_Num=2;
static AtomicInteger result=new AtomicInteger(-1);
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;
}
public static int pSearch(int searchValue) throws ExecutionException, InterruptedException {
int subArrSize = arr.length/Thread_Num+1;
List<Future<Integer>> re = new ArrayList<Future<Integer>>();
for(int i=0;i<arr.length;i++){
int end = i+subArrSize;
if(end>=arr.length)end=arr.length;
re.add(pool.submit(new SearchTask(searchValue,i,end)));
}
for(Future<Integer> fu:re){
if(fu.get()>=0)
return fu.get();
}
return -1;
}
public static class SearchTask implements Callable<Integer> {
int begin,end,searchValue;
public SearchTask(int begin, int end, int searchValue) {
this.begin = begin;
this.end = end;
this.searchValue = searchValue;
}
@Override
public Integer call() throws Exception {
int re = search(searchValue,begin,end);
return re;
}
}
}