Skip to content

Instantly share code, notes, and snippets.

@mgodave
Created April 2, 2012 02:30
Show Gist options
  • Save mgodave/2280120 to your computer and use it in GitHub Desktop.
Save mgodave/2280120 to your computer and use it in GitHub Desktop.
import org.junit.Test;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertFalse;
/**
* Created by IntelliJ IDEA.
* User: rusekd
* Date: 4/1/12
* Time: 12:08 PM
* To change this template use File | Settings | File Templates.
*/
public class ThreadPoolExecutorTest {
public static class MyThreadPoolExecutor extends ThreadPoolExecutor {
private volatile long beforeThreadId;
private volatile long afterThreadId;
public MyThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
afterThreadId = Thread.currentThread().getId();
}
@Override
protected void beforeExecute(Thread t, Runnable r) {
super.beforeExecute(t, r);
beforeThreadId = Thread.currentThread().getId();
}
public long getBeforeThreadId() {
return beforeThreadId;
}
public long getAfterThreadId() {
return afterThreadId;
}
}
@Test
public void testBeforeAndAfterExecuteFromDifferentThread() {
final MyThreadPoolExecutor executor =
new MyThreadPoolExecutor(1, 1, Integer.MAX_VALUE, TimeUnit.DAYS, new SynchronousQueue<Runnable>());
executor.submit(new Runnable() {
public void run() {
}
});
executor.shutdown();
System.out.println(executor.getBeforeThreadId());
System.out.println(executor.getAfterThreadId());
assertFalse(executor.getBeforeThreadId() == executor.getAfterThreadId());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment