Skip to content

Instantly share code, notes, and snippets.

View nschlimm's full-sized avatar
🏠
Working from home

Niklas Schlimm nschlimm

🏠
Working from home
  • TIMOCOM GmbH
  • Cologne, Germany
View GitHub Profile
@nschlimm
nschlimm / gist:2135620
Created March 20, 2012 13:30
Properly opened file channel
ThreadPoolExecutor pool = new ThreadPoolExecutor(5, 5, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(2500));
pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
AsynchronousFileChannel outputfile =
AsynchronousFileChannel.open(Paths.get(FILE_NAME),
new HashSet<StandardOpenOption>(Arrays.asList(StandardOpenOption.WRITE,
StandardOpenOption.CREATE)), pool);
@nschlimm
nschlimm / gist:2134350
Created March 20, 2012 11:36
PoolSizeCalculator
/**
* A class that calculates the optimal thread pool boundaries. It takes the desired target utilization and the desired
* work queue memory consumption as input and retuns thread count and work queue capacity.
*
* @author Niklas Schlimm
*
*/
public abstract class PoolSizeCalculator {
/**
@nschlimm
nschlimm / gist:1959129
Created March 2, 2012 15:19
Custom thread pool
public class Performance_Benchmark_AsynchronousFileChannel_2 implements Runnable {
private static AsynchronousFileChannel outputfile;
private static AtomicInteger fileindex = new AtomicInteger(0);
private static ThreadPoolExecutor pool = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(), new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setDaemon(true);
@nschlimm
nschlimm / gist:1958586
Created March 2, 2012 14:10
OutOfMemoryException
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:691)
at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:943)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1336)
at sun.nio.ch.AsynchronousChannelGroupImpl.executeOnPooledThread(AsynchronousChannelGroupImpl.java:188)
at sun.nio.ch.PendingIoCache.clearPendingIoMap(PendingIoCache.java:158)
at sun.nio.ch.PendingIoCache.close(PendingIoCache.java:115)
at sun.nio.ch.WindowsAsynchronousFileChannelImpl.close(WindowsAsynchronousFileChannelImpl.java:147)
at com.schlimm.java7.nio.Performance_OOM_AsynchronousFileChannel_1.main(Performance_OOM_AsynchronousFileChannel_1.java:36)
@nschlimm
nschlimm / gist:1950522
Created March 1, 2012 15:29
Custom thread pool
public class Performance_Benchmark_AsynchronousFileChannel_2 implements Runnable {
private static AsynchronousFileChannel outputfile;
private static AtomicInteger fileindex = new AtomicInteger(0);
private static ExecutorService pool = Executors.newFixedThreadPool(1, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setDaemon(true);
return t;
@nschlimm
nschlimm / gist:1950482
Created March 1, 2012 15:23
ThreadPool.createDefault
public class ThreadPool {
...
private static final ThreadFactory defaultThreadFactory = new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setDaemon(true);
return t;
}
};
@nschlimm
nschlimm / gist:1950373
Created March 1, 2012 15:08
Benchmark conventional I/O
public class Performance_Benchmark_ConventionalFileAccessExample_1 implements Runnable {
private static FileOutputStream outputfile;
private static byte[] content = "Hello".getBytes();
public static void main(String[] args) throws InterruptedException, IOException {
try {
System.out.println("Test: " + Performance_Benchmark_ConventionalFileAccessExample_1.class.getSimpleName());
outputfile = new FileOutputStream(new File("E:/temp/afile.out"), true);
Average average = new PerformanceHarness().calculatePerf(new PerformanceChecker(1000, new Performance_Benchmark_ConventionalFileAccessExample_1()), 5);
@nschlimm
nschlimm / gist:1950310
Created March 1, 2012 15:01
First benchmark
public class Performance_Benchmark_AsynchronousFileChannel_1 implements Runnable {
private static AsynchronousFileChannel outputfile;
private static int fileindex = 0;
public static void main(String[] args) throws InterruptedException, IOException {
try {
System.out.println("Test: " + Performance_Benchmark_AsynchronousFileChannel_1.class.getSimpleName());
outputfile = AsynchronousFileChannel.open(Paths.get("E:/temp/afile.out"), StandardOpenOption.WRITE,
StandardOpenOption.CREATE, StandardOpenOption.DELETE_ON_CLOSE);
@nschlimm
nschlimm / gist:1950035
Created March 1, 2012 14:08
Plain asynchronous file channel usage
public class CallGraph_Default_AsynchronousFileChannel {
private static AsynchronousFileChannel fileChannel;
public static void main(String[] args) throws InterruptedException, IOException, ExecutionException {
try {
fileChannel = AsynchronousFileChannel.open(Paths.get("E:/temp/afile.out"), StandardOpenOption.READ,
StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.DELETE_ON_CLOSE);
Future<Integer> future = fileChannel.write(ByteBuffer.wrap("Hello".getBytes()), 0L);
future.get();
@nschlimm
nschlimm / gist:1723147
Created February 2, 2012 11:54
String invokevirtual
Constant pool:
#1 = Class #2 // com/schlimm/bytecode/examples/BytecodeExamples
...
#42 = Class #43 // java/lang/String
...
#65 = Methodref #42.#66 // java/lang/String.length:()I
#66 = NameAndType #67:#68 // length:()I
#67 = Utf8 length
#68 = Utf8 ()I
...