|
Java example source code file (MergeDemo.java)
The MergeDemo.java Java example source code
/*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This source code is provided to illustrate the usage of a given feature
* or technique and has been deliberately simplified. Additional steps
* required for a production-quality application, such as security checks,
* input validation and proper error handling, might not be present in
* this sample code.
*/
import java.util.Arrays;
import java.util.Random;
import static java.lang.Integer.parseInt;
/**
* MergeExample is a class that runs a demo benchmark of the {@code ForkJoin} framework
* by benchmarking a {@link MergeSort} algorithm that is implemented using
* {@link java.util.concurrent.RecursiveAction}.
* The {@code ForkJoin} framework is setup with different parallelism levels
* and the sort is executed with arrays of different sizes to see the
* trade offs by using multiple threads for different sizes of the array.
*/
public class MergeDemo {
// Use a fixed seed to always get the same random values back
private final Random random = new Random(759123751834L);
private static final int ITERATIONS = 10;
/**
* Represents the formula {@code f(n) = start + (step * n)} for n = 0 & n < iterations
*/
private static class Range {
private final int start;
private final int step;
private final int iterations;
private Range(int start, int step, int iterations) {
this.start = start;
this.step = step;
this.iterations = iterations;
}
/**
* Parses start, step and iterations from args
* @param args the string array containing the arguments
* @param start which element to start the start argument from
* @return the constructed range
*/
public static Range parse(String[] args, int start) {
if (args.length < start + 3) {
throw new IllegalArgumentException("Too few elements in array");
}
return new Range(parseInt(args[start]), parseInt(args[start + 1]), parseInt(args[start + 2]));
}
public int get(int iteration) {
return start + (step * iteration);
}
public int getIterations() {
return iterations;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(start).append(" ").append(step).append(" ").append(iterations);
return builder.toString();
}
}
/**
* Wraps the different parameters that is used when running the MergeExample.
* {@code sizes} represents the different array sizes
* {@code parallelism} represents the different parallelism levels
*/
private static class Configuration {
private final Range sizes;
private final Range parallelism;
private final static Configuration defaultConfig = new Configuration(new Range(20000, 20000, 10),
new Range(2, 2, 10));
private Configuration(Range sizes, Range parallelism) {
this.sizes = sizes;
this.parallelism = parallelism;
}
/**
* Parses the arguments and attempts to create a configuration containing the
* parameters for creating the array sizes and parallelism sizes
* @param args the input arguments
* @return the configuration
*/
public static Configuration parse(String[] args) {
if (args.length == 0) {
return defaultConfig;
} else {
try {
if (args.length == 6) {
return new Configuration(Range.parse(args, 0), Range.parse(args, 3));
}
} catch (NumberFormatException e) {
System.err.println("MergeExample: error: Argument was not a number.");
}
System.err.println("MergeExample <size start>
Other Java examples (source code examples)Here is a short list of links related to this Java MergeDemo.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.