|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
class IntToString implements Fun<Integer, String> { @Override public String apply(Integer x) { return "" + x; // Integer.toString(x) would be fine, too } } public class Map { private static class Worker<T, R> implements Runnable { private final Fun<T, R> f; private final T[] a; private final R[] b; private final int from; private final int to; Worker(Fun<T, R> f, T[] a, R[] b, int from, int to) { this.f = f; this.a = a; this.b = b; this.from = from; this.to = to; } @Override public void run() { for (int i = from; i < to; ++i) b[i] = f.apply(a[i]); } } public static <T, R> void map(Fun<T, R> f, T[] a, R[] b, int n) throws InterruptedException { if (f == null || a == null || b == null || a.length > b.length || n < 1) throw new IllegalArgumentException(); if (n > a.length) n = a.length; Thread[] worker = new Thread[n]; final int more = a.length % n; final int step = a.length / n; final int step_more = step + 1; final int delta = more * step_more; for (int i = 0; i < more; ++i) { final int from = i * step_more; final int to = from + step_more; worker[i] = new Thread(new Worker<>(f, a, b, from, to)); worker[i].start(); } for (int i = 0; i < (n - more); ++i) { final int from = delta + i * step; final int to = from + step; worker[i + more] = new Thread(new Worker<>(f, a, b, from, to)); worker[i + more].start(); } for (int i = 0; i < n; ++i) worker[i].join(); } } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import java.util.Arrays; public class ParallelverordnungSolution implements Runnable { private int[] numbers; public ParallelverordnungSolution (int[] numbers) { this.numbers = numbers; } void perm (int from) { if (from == numbers.length - 1) { System.out.println(Arrays.toString(numbers)); } else { perm(from + 1); for (int j = from + 1; j < numbers.length; j++) { int t = numbers[j]; numbers[j] = numbers[from]; numbers[from] = t; perm(from + 1); numbers[from] = numbers[j]; numbers[j] = t; } } } @Override public void run () { perm(1); } public static void main (String[] args) { int[] numbers = readArray(); Thread[] threads = new Thread[numbers.length]; for (int i = 0; i < threads.length; i++) { int[] numbersThread = new int[numbers.length]; System.arraycopy(numbers, 0, numbersThread, 0, numbers.length); numbersThread[0] = numbers[i]; numbersThread[i] = numbers[0]; threads[i] = new Thread(new ParallelverordnungSolution(numbersThread)); threads[i].start(); } for(Thread t : threads) try { t.join(); } catch (InterruptedException e) { e.printStackTrace(); } } static int[] readArray() { return new int[]{1, 2, 3, 4, 5, 7, 8, 9, 10}; } } // Ende der Klasse Parallelverordnung |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import java.util.concurrent.locks.ReentrantLock; class SecureCounter extends Thread { private long target[]; // used as reference on value private long amount; // how many times shall we count private ReentrantLock lock; SecureCounter(long target[], ReentrantLock lock, long amount) { this.target = target; this.amount = amount; this.lock = lock; } public void run() { for (long c = 0; c < amount; c++) { lock.lock(); // enter critical section // synchronized(target) { //could use synchronized here // (target is shared between both threads and contains it's own lock) long tmp = target[0]; tmp = tmp + 1; target[0] = tmp; // } lock.unlock(); // synchronized only works inside methods, lock can be held after leaving a // method } } } public class RaceSolution { public static void main(String args[]) throws InterruptedException { long svalue[] = {0}; long sinc = 10000000; // must be big enough! ReentrantLock lock = new ReentrantLock(); SecureCounter sc1 = new SecureCounter(svalue, lock, sinc); SecureCounter sc2 = new SecureCounter(svalue, lock, sinc); // same value, same lock! sc1.start(); sc2.start(); sc1.join(); sc2.join(); System.out .println("secure counter (much slower...why?): expected " + 2 * sinc + " got " + svalue[0]); } } |
Slide of WS13/14: