Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. package mohibmir.linearsort;
  2.  
  3. import java.lang.reflect.Array;
  4. import java.util.Vector;
  5. import java.util.concurrent.ExecutorService;
  6. import java.util.concurrent.Executors;
  7. import java.util.concurrent.ForkJoinPool;
  8.  
  9. public class Main {
  10.  
  11. public static void main(String[] args) {
  12. int[] list = new int[10_000_000];
  13. for(int i = 0; i < 9_999_999; i++){
  14. list[i] = ((int) (Math.random()*1_000_000));
  15. }
  16.  
  17. long x = System.currentTimeMillis();
  18. LinearSearch linearsearch = new LinearSearch(list);
  19. linearsearch.search(list[34321], 0, 9_999_999);
  20. linearsearch.search(-5, 0, 9_999_999);
  21. long timeelapsed = System.currentTimeMillis() - x;
  22.  
  23. System.out.println("Normal linear search took: " + timeelapsed + " milliseconds.");
  24.  
  25. x = System.currentTimeMillis();
  26. LinearSearchMultithreaded ls = new LinearSearchMultithreaded(list);
  27. ls.search(21);
  28. ls.search(-21);
  29. timeelapsed = System.currentTimeMillis() - x;
  30. System.out.println("Multithreaded search took " + timeelapsed + " milliseconds.");
  31.  
  32. x = System.currentTimeMillis();
  33. LinearSearchParallel lsp = new LinearSearchParallel(list);
  34. ls.search(51);
  35.  
  36. timeelapsed = System.currentTimeMillis() - x;
  37. System.out.println("Multithreaded search took " + timeelapsed + " milliseconds.");
  38.  
  39. }
  40. }
  41.  
  42. class LinearSearch extends Thread{
  43. int[] list;
  44. int searchFor;
  45. boolean found;
  46. int ind;
  47. int start;
  48. int end;
  49.  
  50. LinearSearch(int[] list){
  51. this.list = list;
  52. start = 0;
  53. end = list.length-1;
  54. }
  55.  
  56. LinearSearch(int[] list, int searchFor){
  57. this.list = list;
  58. this.searchFor = searchFor;
  59. start = 0;
  60. end = list.length-1;
  61. }
  62.  
  63. LinearSearch(int[] list, int searchFor, int start, int end){
  64. this.list = list;
  65. this.searchFor = searchFor;
  66. this.start = start;
  67. this.end = end;
  68. }
  69.  
  70.  
  71. public void run(){
  72. search(searchFor, start, end);
  73. }
  74.  
  75. public int search(int x, int start, int end){
  76. if(list == null){
  77. return -1;
  78. }
  79. for(int i = start; i <= end; i++){
  80. if(list[i] == x){
  81. System.out.println(i);
  82. found = true;
  83. this.ind = i;
  84. return i;
  85. }
  86. }
  87. if(start == 0 && end == list.length){
  88. System.out.println(-1);
  89. }
  90. found = false;
  91. return -1;
  92. }
  93.  
  94. }
  95.  
  96. class LinearSearchMultithreaded{
  97. int[] list;
  98.  
  99. LinearSearchMultithreaded(int[] list){
  100. this.list = list;
  101. }
  102.  
  103. public void search(int x){
  104. int start = 0;
  105. int factor = (int) Math.ceil((double)(list.length/4));
  106. int end;
  107.  
  108. ExecutorService exec = Executors.newFixedThreadPool(4);
  109. LinearSearch[] th = new LinearSearch[4];
  110.  
  111. for(int i = 0; i < 4; i++){
  112. if(start + factor < list.length){
  113. end = start + factor;
  114. }else{
  115. end = list.length-1;
  116. }
  117. LinearSearch proc = new LinearSearch(list, x, start, end);
  118. th[i] = proc;
  119. exec.execute(proc);
  120. start += factor;
  121. }
  122. exec.shutdown();
  123.  
  124. boolean found = false;
  125.  
  126. while(!exec.isTerminated()){}
  127.  
  128. // if(exec.isTerminated()){
  129. // for(int i = 0; i < 4; i++){
  130. // if(th[i].found){
  131. // found = true;
  132. // }
  133. // }
  134. //
  135. // if(!found){
  136. // System.out.println("-1");
  137. // }
  138. // }
  139.  
  140. }
  141.  
  142. /*private int splitSearch(int[] list, int start, int end, int result){
  143. int[] v = new int[]();
  144. Runnable search = new Runnable(){
  145.  
  146. int index = -1;
  147. public void run(){
  148. for(int i = start; i < end; i++){
  149. if(list.get(i) == result){
  150. index = i;
  151. v.add(index);
  152. break;
  153. }
  154. }
  155.  
  156. }
  157. };
  158.  
  159. if(v.isEmpty()){
  160. return -1;
  161. }else{
  162. return v.get(0);
  163. }
  164. }*/
  165. }
  166.  
  167. class LinearSearchParallel {
  168. int[] list;
  169.  
  170. LinearSearchParallel(int[] list){
  171. this.list = list;
  172. }
  173.  
  174.  
  175.  
  176. public void search(int x){
  177. int start = 0;
  178. int factor = (int) Math.ceil((double)(list.length/4));
  179. int end;
  180.  
  181. ForkJoinPool pool = new ForkJoinPool();
  182. LinearSearch[] threads = new LinearSearch[4];
  183.  
  184. for(int i = 0; i < 4; i++){
  185. if(start + factor < list.length){
  186. end = start + factor;
  187. }else{
  188. end = list.length-1;
  189. }
  190. threads[i] = new LinearSearch(list, x, start, end);
  191. pool.execute(threads[i]);
  192. start += factor;
  193. }
  194. pool.shutdown();
  195.  
  196. }
  197.  
  198. // public static long parallelMergeSort(int[] list, int proc) {
  199. // 24 long before = System.currentTimeMillis();
  200. // 25 ForkJoinPool pool = new ForkJoinPool(proc);
  201. // 26 pool.invoke(new SortTask(list));
  202. // 27 pool.shutdown();
  203. // 28 while (!pool.isTerminated()) {
  204. // 29 Thread.yield();
  205. // 30 }
  206. // 31 long after = System.currentTimeMillis();
  207. // 32 long time = after - before;
  208. // 33 return time;
  209. // 34 }
  210. // • Parallel Computing USC CSCI 201L 14/16
  211. // 35 private static class SortTask extends RecursiveAction {
  212. // 36 public static final long serialVersionUID = 1;
  213. // 37 private int[] list;
  214. // 38 SortTask(int[] list) {
  215. // 39 this.list = list;
  216. // 40 }
  217. // 41
  218. // 42 protected void compute() {
  219. // 43 if (list.length < 2) return; // base case
  220. // 44 // split into halves
  221. // 45 int[] firstHalf = new int[list.length / 2];
  222. // 46 System.arraycopy(list, 0, firstHalf, 0, list.length / 2);
  223. // 47 int secondLength = list.length - list.length / 2;
  224. // 48 int[] secondHalf = new int[secondLength];
  225. // 49 System.arraycopy(list, list.length / 2, secondHalf, 0, secondLength);
  226. // 50
  227. // 51 // recursively sort the two halves
  228. // 52 SortTask st1 = new SortTask(firstHalf);
  229. // 53 SortTask st2 = new SortTask(secondHalf);
  230. // 54 st1.fork();
  231. // 55 st2.fork();
  232. // 56 st1.join();
  233. // 57 st2.join();
  234. // 58 }
  235. // 59 }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement