This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void attempt1(final double[] array) { | |
Thread[] threads = new Thread[NTHREADS - 1]; | |
final int segmentLen = array.length / NTHREADS; | |
int offset = 0; | |
for (int i = 0; i < NTHREADS - 1; i++) { | |
final int from = offset; | |
final int to = offset + segmentLen; | |
threads[i] = new Thread(new Runnable() { | |
@Override |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void work(double[] array, int from, int to) { | |
for (int j = from; j < to; j++) { | |
array[j] = Math.log(j); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.ForkJoinPool; | |
import java.util.concurrent.RecursiveAction; | |
import java.util.concurrent.TimeUnit; | |
public class Stealing { | |
private final static int NTHREADS = Runtime.getRuntime().availableProcessors(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Field; | |
public class BigArray { | |
public static sun.misc.Unsafe getUnsafe() { | |
try { | |
Field f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); | |
f.setAccessible(true); | |
return (sun.misc.Unsafe) f.get(null); | |
} catch (Exception e) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
long size = ((long) Integer.MAX_VALUE) + 10; | |
BigIntArray bigArray = new BigIntArray(size); | |
bigArray.set(size - 1, 1337); | |
System.out.println(bigArray.get(size - 1)); // prints 1337 | |
bigArray.free(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BigIntArray { | |
private long address; | |
private final static int INT_BYTE_SIZE = 4; | |
public BigIntArray(long size) { | |
address = getUnsafe().allocateMemory(size * INT_BYTE_SIZE); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static sun.misc.Unsafe getUnsafe() { | |
try { | |
Field f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); | |
f.setAccessible(true); | |
return (sun.misc.Unsafe) f.get(null); | |
} catch (Exception e) { /* ... */ } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.atomic.AtomicInteger; | |
class MyAtomicInteger extends AtomicInteger { | |
public void mult(int val) { | |
int olvVal, newVal; | |
do { | |
oldVal = get(); | |
newVal = oldVal * val; | |
} while(compareAndSet(oldVal, newVal) == false); |
NewerOlder