Uno scrittoio digitale …

28 aprile 2015

Ninna nanna per Lorenzo

Archiviato in: Senza categoria — Tag: — admin @ 22:09

ninna nanna

21 aprile 2015

Java7 ForkJoin Framework

Archiviato in: Senza categoria — Tag:, , — admin @ 11:17

For learning about basic parallel operations, there are only 2-4 classes you need to know about:

  • ForkJoinPool: you create exactly one of these to run all your fork-join tasks in the whole program
  • RecursiveTask: you run a subclass of this in a pool and have it return a result; see the examples below
  • RecursiveAction: just like RecursiveTask except it does not return a result
  • ForkJoinTask: superclass of RecursiveTask and RecursiveAction. fork and join are methods defined in this class. You won’t use this class directly, but it is the class with most of the useful javadoc documentation, in case you want to learn about additional methods

import java.util.Random;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

public class MaximumFinder extends RecursiveTask {

  private static final int SEQUENTIAL_THRESHOLD = 5;

  private final int[] data;
  private final int start;
  private final int end;

  public MaximumFinder(int[] data, int start, int end) {
    this.data = data;
    this.start = start;
    this.end = end;
  }

  public MaximumFinder(int[] data) {
    this(data, 0, data.length);
  }

  @Override
  protected Integer compute() {
    final int length = end - start;
    if (length < SEQUENTIAL_THRESHOLD) {
      return computeDirectly();
    }
    final int split = length / 2;
    final MaximumFinder left = new MaximumFinder(data, start, start + split);
    left.fork();
    final MaximumFinder right = new MaximumFinder(data, start + split, end);
    return Math.max(right.compute(), left.join());
  }

  private Integer computeDirectly() {
    System.out.println(Thread.currentThread() + ' computing: ' + start
                       + ' to ' + end);
    int max = Integer.MIN_VALUE;
    for (int i = start; i < end; i++) {
      if (data[i] > max) {
        max = data[i];
      }
    }
    return max;
  }

  public static void main(String[] args) {
    // create a random data set
    final int[] data = new int[1000];
    final Random random = new Random();
    for (int i = 0; i < data.length; i++) {
      data[i] = random.nextInt(100);
    }

    // submit the task to the pool
    final ForkJoinPool pool = new ForkJoinPool(4);
    final MaximumFinder finder = new MaximumFinder(data);
    System.out.println(pool.invoke(finder));
  }
}

19 aprile 2015

Measure Anything, Measure Everything

Archiviato in: code — Tag:, — admin @ 16:01

statsd

13 aprile 2015

Run your code online

Archiviato in: code — Tag: — admin @ 10:14

http://runnable.com/

Fatto veramente bene e molto semplice da usare.

Funziona con WordPress