Skip to content

Instantly share code, notes, and snippets.

@jreyes
Created January 14, 2012 06:17
Show Gist options
  • Save jreyes/1610499 to your computer and use it in GitHub Desktop.
Save jreyes/1610499 to your computer and use it in GitHub Desktop.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolExample
{
// --------------------------- main() method ---------------------------
public static void main( String[] args )
{
final long start = System.currentTimeMillis();
ExecutorService executor = Executors.newFixedThreadPool( 1 );
for ( int i = 0; i < 10; i++ )
{
final int threadId = i;
executor.execute( new Runnable()
{
public void run()
{
try
{
Thread.sleep( 10 );
long duration = System.currentTimeMillis() - start;
System.out.println( duration + " - finishing thread " + threadId );
}
catch ( InterruptedException ignored )
{
}
}
} );
}
executor.shutdown();
// Wait until all threads are finish
while ( !executor.isTerminated() )
{
}
System.out.println( "Finished all threads" );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment