Skip to content

Instantly share code, notes, and snippets.

View omsn's full-sized avatar

Korbi Schmid omsn

  • Oracle
  • Austin, TX
View GitHub Profile
@omsn
omsn / attempt1.java
Last active December 11, 2015 22:28
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
private void work(double[] array, int from, int to) {
for (int j = from; j < to; j++) {
array[j] = Math.log(j);
}
}
@omsn
omsn / Stealing.java
Last active August 19, 2017 17:43
Testing different approaches how to parallelize for-each loops
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();
@omsn
omsn / BigArray.java
Last active February 21, 2025 18:59
full source code of how to create big arrays
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) {
@omsn
omsn / test-big-array.java
Last active December 11, 2015 16:59
testing big array
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();
@omsn
omsn / BigIntArray.java
Last active December 11, 2015 16:59
big array
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);
}
@omsn
omsn / obtain-unsafe.java
Last active February 21, 2025 18:49
obtaining sun.misc.Unsafe
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) { /* ... */ }
}
@omsn
omsn / gist:4554084
Created January 17, 2013 06:16
extending AtomicInteger with an atomic, non-blocking multiply method
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);