import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
+import com.google.common.base.Stopwatch;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
-
import org.junit.After;
import org.junit.Test;
-import com.google.common.base.Stopwatch;
-
/**
* Tests various ThreadPoolExecutor implementations.
*
@After
public void tearDown() {
- if( executor != null ) {
+ if (executor != null) {
executor.shutdownNow();
}
}
100000, "TestPool", 0 );
}
- @Test(expected=RejectedExecutionException.class)
+ @Test(expected = RejectedExecutionException.class)
public void testFastThreadPoolRejectingTask() throws Exception {
executor = SpecialExecutors.newBoundedFastThreadPool( 1, 1, "TestPool" );
- for( int i = 0; i < 5; i++ ) {
+ for (int i = 0; i < 5; i++) {
executor.execute( new Task( null, null, null, null,
TimeUnit.MICROSECONDS.convert( 5, TimeUnit.SECONDS ) ) );
}
100000, "TestPool", 0 );
}
- @Test(expected=RejectedExecutionException.class)
+ @Test(expected = RejectedExecutionException.class)
public void testCachedThreadRejectingTask() throws Exception {
ExecutorService executor = SpecialExecutors.newBoundedCachedThreadPool( 1, 1, "TestPool" );
- for( int i = 0; i < 5; i++ ) {
+ for (int i = 0; i < 5; i++) {
executor.execute( new Task( null, null, null, null,
TimeUnit.MICROSECONDS.convert( 5, TimeUnit.SECONDS ) ) );
}
this.executor = executor;
- System.out.println( "\nTesting " + executor.getClass().getSimpleName() + " with " +
- numTasksToRun + " tasks." );
+ System.out.println("\nTesting " + executor.getClass().getSimpleName() + " with " + numTasksToRun + " tasks.");
final CountDownLatch tasksRunLatch = new CountDownLatch( numTasksToRun );
final ConcurrentMap<Thread, AtomicLong> taskCountPerThread = new ConcurrentHashMap<>();
final AtomicReference<AssertionError> threadError = new AtomicReference<>();
- Stopwatch stopWatch = new Stopwatch();
- stopWatch.start();
+ Stopwatch stopWatch = Stopwatch.createStarted();
new Thread() {
@Override
public void run() {
- for( int i = 0; i < numTasksToRun; i++ ) {
-// if(i%100 == 0) {
+ for (int i = 0; i < numTasksToRun; i++) {
+// if (i%100 == 0) {
// Uninterruptibles.sleepUninterruptibly( 20, TimeUnit.MICROSECONDS );
// }
stopWatch.stop();
- if( !done ) {
- fail( (numTasksToRun - tasksRunLatch.getCount()) + " tasks out of " +
- numTasksToRun + " executed" );
+ if (!done) {
+ fail((numTasksToRun - tasksRunLatch.getCount()) + " tasks out of " + numTasksToRun + " executed");
}
- if( threadError.get() != null ) {
+ if (threadError.get() != null) {
throw threadError.get();
}
System.out.println( taskCountPerThread.size() + " threads used:" );
- for( Map.Entry<Thread, AtomicLong> e : taskCountPerThread.entrySet() ) {
+ for (Map.Entry<Thread, AtomicLong> e : taskCountPerThread.entrySet()) {
System.out.println( " " + e.getKey().getName() + " - " + e.getValue() + " tasks" );
}
public void run() {
try {
try {
- if( delay > 0 ) {
+ if (delay > 0) {
TimeUnit.MICROSECONDS.sleep( delay );
- } else if( blockLatch != null ) {
+ } else if (blockLatch != null) {
blockLatch.await();
}
- } catch( InterruptedException e ) {}
+ } catch (InterruptedException e) {
+ }
- if( expThreadPrefix != null ) {
+ if (expThreadPrefix != null) {
assertEquals( "Thread name starts with " + expThreadPrefix, true,
Thread.currentThread().getName().startsWith( expThreadPrefix ) );
}
- if( taskCountPerThread != null ) {
+ if (taskCountPerThread != null) {
AtomicLong count = taskCountPerThread.get( Thread.currentThread() );
- if( count == null ) {
+ if (count == null) {
count = new AtomicLong( 0 );
AtomicLong prev = taskCountPerThread.putIfAbsent( Thread.currentThread(), count );
- if( prev != null ) {
+ if (prev != null) {
count = prev;
}
}
count.incrementAndGet();
}
- } catch( AssertionError e ) {
- if( threadError != null ) {
+ } catch (AssertionError e) {
+ if (threadError != null) {
threadError.set( e );
}
} finally {
- if( tasksRunLatch != null ) {
+ if (tasksRunLatch != null) {
tasksRunLatch.countDown();
}
}