Java Programing There are NO static variables allowed in this lab. The only clas
ID: 3740167 • Letter: J
Question
Java Programing
There are NO static variables allowed in this lab. The only class that contains a static method is the primary test class. Any member variables are private. Output statements are only in the test program.
The focus of this lab is to demonstrate the power of using threads to improve execution times. Whenever a compute intensive task can be broken into a collection of smaller tasks, each of which can be executed within a separate thread, total execution time to complete the task will be significantly improved.
The approach of this lab will be to create a compute intensive task which can easily be broken down into separate subtasks. Execution time for this task will be measured and reported. The task will then be implemented such that a collection of threads can execute the subtasks. Execution time for the collection of threads to complete the task will be measured and reported. If things are done correctly, the multithreaded execution time reported will be significantly faster than the single threaded execution time.
The task to be measured will be the sorting of 3 ArrayLists containing double values. The size of the ArrayLists should be controlled by a constant which is defined to be 1000000. For the single threaded case, the strategy will be:
Get time task starts
Sort first list
Sort second list
Sort third list
Get time task ends
Total time = end time – start time
For the multithreaded case, the strategy will be:
Create thread to sort first list
Create thread to sort second list
Create thread to sort third list
Get time task starts
Start the three threads
Wait for all three threads to complete
Get time task ends
Total time = end time – start time
Getting the current time in Java is accomplished by making the following function call:
long time = System.currentTimeMillis( );
This function returns the current time as a long value which represents the number of milliseconds from a fixed reference date. Subtracting a start time from and end time represents the number of milliseconds which elapsed between the start and end times. (Note: there are 1000 milliseconds in 1 second).
Trying to get reasonable timing data from Java is a real challenge. If each of elapsed time measurements are repeated several times, the times reported would vary considerably. Java does things behind the scenes which can take time away from the task being measured. The operating system does things behind the scenes which can take time away from the task being measured. As a user on the system, you can do things such as moving the mouse or running another application while the task you are measuring is being timed which will interfere with the completion of the task being measured.
In order to try to compensate for these things which cannot be controlled, the measurement strategy will be to repeat the exact same task some number of times, summing the execution times, then dividing the sum by the number of repetitions. This will produce an average which will be more indicative of the amount of time the task actually takes. Even this average will not be exact, and will vary each time the test is ran. However, the amount of variance will be significantly less than the variance between individual measurements. To summarize this averaging strategy:
Initialize totalTime variable
Repeat the following some number of reps
Get copy of list1
Get copy of list2
Get copy of list3
Add time to complete sorting task to totalTime
Average time = totalTime / number of reps
The number of reps should be defined as a constant so it can be easily controlled. Set this to 25 initially. Running this test 25 times will likely take a couple minutes. While the program is running, DO NOT do anything else on the computer. Don’t even move the mouse. This will help reduce the outside interference on the time it takes the program to complete. The following is the output from running this program on a typical computer:
Single threaded sorting took 2485 ms
Multi threaded sorting took 581 ms
Your numbers will certainly be different than these. However, you should be a similar improvement going from single threaded execution to multithreaded execution.
Make sure that everything is properly documented with class and method level Javadoc comments.
Explanation / Answer
Answer)
start time can be calculated as long startTime=System.currentTimeMillis( );
List<double> l1=new ArrayList<>(1000000);
// you can insert element in list1
List<double> l2=new ArrayList<>(1000000);
// you can insert element in list2
List<double> l2=new ArrayList<>(1000000);
// you can insert element in list3
Collections.sort(l1); // sort first list1
Collections.sort(l2); // sort first list2
Collections.sort(l3); // sort first list3
long endTime = System.currentTimeMillis( ); // end time
System.out.println("total time is ",endTime - startTime); // time taken to sort
And for thread you can create a thread and allocate different list in different thread to be sort and after thread are completed log the time and calculate the difference .
// thread class
}
// main class to create thread and note start and end time
long startTime=System.currentTimeMillis( ); // start time
t1.start(); //starting thread 1
t2.start(); //starting thread 2
t3.start(); //starting thread 3
long endTime = System.currentTimeMillis( ); // end time
System.out.println("total time is ",endTime - startTime); // time taken to sort
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.