Skip to content

Instantly share code, notes, and snippets.

View RuedigerMoeller's full-sized avatar
💭
I may be slow to respond.

moru0011 RuedigerMoeller

💭
I may be slow to respond.
View GitHub Profile
abstract class ByteStream { .. }
class SomeClass {
void someMethod() {
this.getStream().writeByte(13); // call on interface or abstract class
}
ByteStream getStream() { .. }
}
// later in code
@RuedigerMoeller
RuedigerMoeller / KontrActorPi.java
Last active August 29, 2015 14:07
Famous Pi Bench with Kontraktor 2.0
public class KontrActorPi {
public static class PiActor extends Actor<PiActor> {
public void $calculatePiFor(int start, int nrOfElements, Adder adder) {
double acc = 0.0;
for (int i = start * nrOfElements; i <= ((start + 1) * nrOfElements - 1); i++) {
acc += 4.0 * (1 - (i % 2) * 2) / (2 * i + 1);
}
adder.$add(acc);
}
@RuedigerMoeller
RuedigerMoeller / TClient.java
Created September 27, 2014 22:12
running a blocking call from inside an actor
exec( () -> {
try {
return new Promise(new URL("http://www.google.com").getContent());
} catch (IOException e) {
return new Promise(null, e);
}
}).then( (content, error) -> {
// handle result
});
@RuedigerMoeller
RuedigerMoeller / TClient.java
Last active August 29, 2015 14:06
emulation of scala's async macro
private static void testRun(RLTable<TCRecord> table, TCMutator mutator, ClientActor client) {
async(
() -> mutator.$init(table),
() -> {
client.$run(table);
return mutator.$run(table, 1000, null);
},
() -> table.$sync(),
() -> client.$unsubscribe(table),
() -> client.$checkCorrectness(table)
@RuedigerMoeller
RuedigerMoeller / gist:8659877
Created January 27, 2014 23:59
improved (my m.barker) disruptor pi bench
public class DisruptorTest2 {
public static class PiJob {
public double result;
public int sliceNr;
public int numIter;
public int partionId;
public void calculatePi() {
double acc = 0.0;
@RuedigerMoeller
RuedigerMoeller / gist:8659844
Created January 27, 2014 23:55
naive implementation with disruptor
public class DisruptorTest {
public static class PiJob {
public double result;
public int sliceNr;
public int numIter;
public void calculatePi() {
double acc = 0.0;
for (int i = sliceNr * numIter; i <= ((sliceNr + 1) * numIter - 1); i++) {
public class DisruptorTest {
public static class PiJob {
public double result;
public int sliceNr;
public int numIter;
public void calculatePi() {
double acc = 0.0;
for (int i = sliceNr * numIter; i <= ((sliceNr + 1) * numIter - 1); i++) {
@RuedigerMoeller
RuedigerMoeller / gist:8273494
Last active January 2, 2016 07:59
Abstraktor version of Akka Pi Sample
public class ActorPiSample {
public static class PiActor extends Actor {
public void calculatePiFor(int start, int nrOfElements, ChannelActor result ) {
double acc = 0.0;
for (int i = start * nrOfElements; i <= ((start + 1) * nrOfElements - 1); i++) {
acc += 4.0 * (1 - (i % 2) * 2) / (2 * i + 1);
}
result.receiveResult(acc);
}
@RuedigerMoeller
RuedigerMoeller / gist:8273307
Last active January 2, 2016 07:59
Akka Pi Sample as multi threaded impl
public class ThreadPi {
static double calculatePiFor(int slice, int nrOfIterations) {
double acc = 0.0;
for (int i = slice * nrOfIterations; i <= ((slice + 1) * nrOfIterations - 1); i++) {
acc += 4.0 * (1 - (i % 2) * 2) / (2 * i + 1);
}
return acc;
}
@RuedigerMoeller
RuedigerMoeller / gist:8273032
Created January 5, 2014 20:01
calc PI slice
static double calculatePiFor(int slice, int nrOfIterations) {
double acc = 0.0;
for (int i = slice * nrOfIterations; i <= ((slice + 1) * nrOfIterations - 1); i++) {
acc += 4.0 * (1 - (i % 2) * 2) / (2 * i + 1);
}
return acc;
}