Markou moment
This commit is contained in:
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -90,3 +90,4 @@
 | 
				
			|||||||
/Semester 3/Assignments/Ajax6_getters_setters_CalebFontenot/target/
 | 
					/Semester 3/Assignments/Ajax6_getters_setters_CalebFontenot/target/
 | 
				
			||||||
/Semester 3/Assignments/mavenproject1/target/
 | 
					/Semester 3/Assignments/mavenproject1/target/
 | 
				
			||||||
/Semester 3/Assignments/Templates01_CalebFontenot/target/
 | 
					/Semester 3/Assignments/Templates01_CalebFontenot/target/
 | 
				
			||||||
 | 
					/Semester 3/Assignments/MaxTask/target/
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -16,8 +16,8 @@ public class MaxTask {
 | 
				
			|||||||
       
 | 
					       
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    public static int max(int[] list) {
 | 
					    public static int max(int[] list) {
 | 
				
			||||||
        RecursiveTask<Integer> task = new MaxTask(list 0, list.length) {
 | 
					        //RecursiveTask<Integer> task = new MaxTask(list 0, list.length) {
 | 
				
			||||||
 | 
					     return 0;   
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
@@ -0,0 +1,94 @@
 | 
				
			|||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 | 
				
			||||||
 | 
					 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					package edu.slcc.asdv.caleb.maxtask;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @author caleb
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					import java.util.concurrent.*;
 | 
				
			||||||
 | 
					public class ParallelMax
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public static void main(String[] args)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        // Create a list
 | 
				
			||||||
 | 
					        final int N = 200000000;
 | 
				
			||||||
 | 
					        int[] list = new int[N];
 | 
				
			||||||
 | 
					        for (int i = 0; i < list.length; i++)
 | 
				
			||||||
 | 
					            list[i] = i;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        long startTime = System.currentTimeMillis();
 | 
				
			||||||
 | 
					        System.out.println("\nPaaraller\nThe maximal number is " + max(list));
 | 
				
			||||||
 | 
					        long endTime = System.currentTimeMillis();      
 | 
				
			||||||
 | 
					        System.out.println("Time with " + (endTime - startTime)
 | 
				
			||||||
 | 
					                + " milliseconds");
 | 
				
			||||||
 | 
					              System.out.println("Number of processors is "
 | 
				
			||||||
 | 
					                + Runtime.getRuntime().availableProcessors());
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					      System.out.println("-------------------------------");
 | 
				
			||||||
 | 
					         startTime = System.currentTimeMillis();
 | 
				
			||||||
 | 
					        int maximum = -1;
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					         for (int i = 0; i < list.length; i++)
 | 
				
			||||||
 | 
					            if (  list[i] > maximum )
 | 
				
			||||||
 | 
					                maximum = list[i];          
 | 
				
			||||||
 | 
					         endTime = System.currentTimeMillis();  
 | 
				
			||||||
 | 
					        System.out.println("Sequential\nThe maximal number is "  + maximum) ;
 | 
				
			||||||
 | 
					        System.out.println("Time NOT in paraller " + (endTime - startTime)
 | 
				
			||||||
 | 
					                + " milliseconds");      
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    public static int max(  int[] list   )
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        RecursiveTask<Integer> task = new MaxTask(list, 0, list.length);
 | 
				
			||||||
 | 
					        ForkJoinPool pool = new ForkJoinPool();
 | 
				
			||||||
 | 
					        return pool.invoke( task   );
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private static class MaxTask 
 | 
				
			||||||
 | 
					                    extends RecursiveTask<Integer>
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        private final static int THRESHOLD = 25000;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        private int[] list;
 | 
				
			||||||
 | 
					        private int low;
 | 
				
			||||||
 | 
					        private int high;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public MaxTask(int[] list, int low, int high)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            this.list = list;
 | 
				
			||||||
 | 
					            this.low = low;
 | 
				
			||||||
 | 
					            this.high = high;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        @Override
 | 
				
			||||||
 | 
					        public Integer compute()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            if (  high-low  <  THRESHOLD)//not parallel
 | 
				
			||||||
 | 
					              {
 | 
				
			||||||
 | 
					                int max = list[0];
 | 
				
			||||||
 | 
					                for (int i = low; i < high; i++)
 | 
				
			||||||
 | 
					                    if (list[i] > max)
 | 
				
			||||||
 | 
					                        max = list[i];
 | 
				
			||||||
 | 
					                return new Integer(max);
 | 
				
			||||||
 | 
					              }
 | 
				
			||||||
 | 
					            else
 | 
				
			||||||
 | 
					              {
 | 
				
			||||||
 | 
					                int mid = (low + high) / 2;
 | 
				
			||||||
 | 
					                RecursiveTask<Integer> left = new MaxTask(list, low, mid);
 | 
				
			||||||
 | 
					                RecursiveTask<Integer> right = new MaxTask(list, mid+1, high);
 | 
				
			||||||
 | 
					                right.fork();
 | 
				
			||||||
 | 
					                left.fork();             
 | 
				
			||||||
 | 
					                return new Integer(Math.max(   left.join().intValue(),
 | 
				
			||||||
 | 
					                                               right.join().intValue())
 | 
				
			||||||
 | 
					                                  );
 | 
				
			||||||
 | 
					              }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user