Created
January 14, 2012 06:17
-
-
Save jreyes/1610499 to your computer and use it in GitHub Desktop.
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
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