Last active
August 29, 2015 14:01
-
-
Save sameei/f3ac9ddfd9ec7a60cced to your computer and use it in GitHub Desktop.
I ask a question in stackoverflow( http://stackoverflow.com/questions/23557550/control-over-termination-threads-in-java-executor-framework ) and get its answer. by attention to the answer i write this code
This file contains hidden or 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
// in the name of ALLAH | |
import java.util.concurrent.*; | |
public class ForkJoinCustomization { | |
public static void main(String[] args) { | |
final SimpleValue val = new SimpleValue(); | |
ExecutorService executor = new ForkJoinPool( | |
3, | |
new WorkerThreadFactory(new Runnable() { | |
@Override | |
public void run() { | |
val.close(); | |
System.out.printf("Close resource on (%s)\n", Thread.currentThread().getName()); | |
} | |
}), | |
null, | |
false // asyncMood | |
); | |
for(int i=0; i<10; i++){ | |
executor.execute(new Task(val)); | |
} | |
executor.shutdown(); | |
while( true ) { | |
try { | |
if( executor.awaitTermination(1, TimeUnit.SECONDS) ) System.exit(0); | |
} catch(InterruptedException intrExc) { | |
// continue... | |
} | |
} | |
} | |
protected static class WorkerThread | |
extends ForkJoinWorkerThread | |
{ | |
protected Runnable _finalizer; | |
protected WorkerThread(ForkJoinPool pool, Runnable finalizer) { | |
super(pool); | |
_finalizer = finalizer; | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
} | |
@Override | |
protected void onTermination(Throwable thrown) { | |
_finalizer.run(); | |
super.onTermination(thrown); | |
} | |
} | |
protected static class WorkerThreadFactory | |
implements ForkJoinPool.ForkJoinWorkerThreadFactory | |
{ | |
protected Runnable threadFinalizer; | |
public WorkerThreadFactory(Runnable finalizer) { | |
super(); | |
threadFinalizer = finalizer; | |
} | |
@Override | |
public ForkJoinWorkerThread newThread(ForkJoinPool forkJoinPool) { | |
return new WorkerThread(forkJoinPool, threadFinalizer); | |
} | |
} | |
protected static interface ResourceProvider<T> | |
extends AutoCloseable | |
{ | |
public T get(); | |
public ResourceProvider<T> reset() throws Exception; | |
public ResourceProvider<T> reset(boolean force) throws Exception; | |
public void close(); | |
} | |
protected static abstract class ThreadLocalResourceProvider<T> | |
extends ThreadLocal<T> | |
implements ResourceProvider<T> {} | |
protected static class SimpleValue | |
extends ThreadLocalResourceProvider<String> | |
{ | |
public String initialValue() { | |
return "Hello " + Thread.currentThread().getName(); | |
} | |
public SimpleValue reset() throws Exception { | |
return reset(false); | |
} | |
public SimpleValue reset(boolean force) throws Exception{ | |
set(this.initialValue()); | |
return this; | |
} | |
public void close() { | |
remove(); | |
} | |
} | |
protected static class Task | |
implements Runnable | |
{ | |
protected SimpleValue val; | |
public Task(SimpleValue val) { | |
this.val = val; | |
} | |
@Override | |
public void run() { | |
try { | |
System.out.println(val.reset().get()); | |
} catch( Exception exc ) { | |
System.out.println(exc.getMessage()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment