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:2301471
Created April 4, 2012 14:18
GracefulAsynchronousFileChannel
/**
* Custom file channel that supports the following requirements:<br>
* - if a write task was submitted successfully, guarantee that the write operation is executed<br>
* - if the channel is closing then throw {@link NonWritableChannelException} to notify the client, that this channel
* isn't writable anymore
*
* @author Niklas Schlimm
*
*/
public class GracefulAsynchronousFileChannel extends AsynchronousFileChannel {
@nschlimm
nschlimm / gist:2234464
Created March 29, 2012 07:13
ThreadLocalUtil
public class ThreadLocalUtil {
private final static ThreadLocal<ThreadVariables> THREAD_VARIABLES = new ThreadLocal<ThreadVariables>() {
/**
* @see java.lang.ThreadLocal#initialValue()
*/
@Override
protected ThreadVariables initialValue() {
return new ThreadVariables();
@nschlimm
nschlimm / gist:2157560
Created March 22, 2012 10:14
Ideal Threadpool for AsynchronousTask
ThreadPoolExecutor pool =
new ThreadPoolExecutor(6, 6,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(2500));
pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
@nschlimm
nschlimm / gist:2157558
Created March 22, 2012 10:14
Use PoolSizeCalculator
public class MyPoolSizeCalculator extends PoolSizeCalculator {
public static void main(String[] args) throws InterruptedException,
InstantiationException,
IllegalAccessException,
ClassNotFoundException {
MyThreadSizeCalculator calculator = new MyThreadSizeCalculator();
calculator.calculateBoundaries(new BigDecimal(1.0),
new BigDecimal(100000));
}
@nschlimm
nschlimm / gist:2157556
Created March 22, 2012 10:13
Custom thread pools
ThreadPoolExecutor pool =
new ThreadPoolExecutor(0, 50,
60, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
ThreadPoolExecutor pool =
new ThreadPoolExecutor(50, 50,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(100000));
@nschlimm
nschlimm / gist:2157553
Created March 22, 2012 10:12
Executors thread pools
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
@nschlimm
nschlimm / gist:2157551
Created March 22, 2012 10:12
ThreadPoolExecutor contsructor
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue&lt;Runnable&gt; workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) { ... }
@nschlimm
nschlimm / gist:2146334
Created March 21, 2012 11:35
Closing with CompletionHandler
public class SimpleChannelClose_CompletionHandler {
...
public static void main(String[] args) throws InterruptedException, IOException, ExecutionException {
...
outputfile.write(ByteBuffer.wrap("Hello".getBytes()), fileindex.getAndIncrement() * 5, "", defaultCompletionHandler);
...
}
private static CompletionHandler<Integer, String> defaultCompletionHandler = new CompletionHandler<Integer, String>() {
@Override
@nschlimm
nschlimm / gist:2137930
Created March 20, 2012 16:29
Ordinary channel close
public class SimpleChannelClose_AsynchronousCloseException {
private static final String FILE_NAME = "E:/temp/afile.out";
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>());
public static void main(String[] args) throws InterruptedException, IOException, ExecutionException {
outputfile = AsynchronousFileChannel.open(
@nschlimm
nschlimm / gist:2135968
Created March 20, 2012 14:05
GracefullChannelCloser
/**
* Graceful shutdown that garantees that submitted tasks will be processed prior shutdown and that denies submission of
* new tasks during "prepare-shutdown" phase.
*
* @author Niklas Schlimm
*
*/
public class SimpleChannelClose_Graceful {
private static final String FILE_NAME = "E:/temp/afile.out";