This file contains 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
/** | |
* 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 { |
This file contains 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 ThreadLocalUtil { | |
private final static ThreadLocal<ThreadVariables> THREAD_VARIABLES = new ThreadLocal<ThreadVariables>() { | |
/** | |
* @see java.lang.ThreadLocal#initialValue() | |
*/ | |
@Override | |
protected ThreadVariables initialValue() { | |
return new ThreadVariables(); |
This file contains 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
ThreadPoolExecutor pool = | |
new ThreadPoolExecutor(6, 6, | |
0L, TimeUnit.MILLISECONDS, | |
new LinkedBlockingQueue<Runnable>(2500)); | |
pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); |
This file contains 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 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)); | |
} |
This file contains 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
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)); |
This file contains 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 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>()); |
This file contains 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 ThreadPoolExecutor(int corePoolSize, | |
int maximumPoolSize, | |
long keepAliveTime, | |
TimeUnit unit, | |
BlockingQueue<Runnable> workQueue, | |
ThreadFactory threadFactory, | |
RejectedExecutionHandler handler) { ... } |
This file contains 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 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 |
This file contains 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 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( |
This file contains 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
/** | |
* 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"; |