package download; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.List; import java.util.Queue; import java.util.concurrent.Executor; import java.util.concurrent.Executors; public class DowloaderTest { private static final int numberOfDownloads = 2; public void execute() { Executor executor = Executors.newFixedThreadPool (DowloaderTest.numberOfDownloads); final List downloads = new LinkedList(); for (int i = 0; i < 99; ++i) { Download d = new Download(i); executor.execute(d); downloads.add(d); } Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { for (Download d: downloads) { d.shutDown(); } } }); //System.exit(0); } /** * @param args */ public static void main(String[] args) { DowloaderTest d = new DowloaderTest(); d.execute(); } private class Download implements Runnable { private Process proc; private final int number; public Download(int number) { this.number= number; } @Override public void run() { System.out.println("starting: " + number); long j = 0; try { Thread.sleep((int)( 10000 * Math.random() + 10000)); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // for (int i = 0; i < 9999; ++i) { // j += 4 * i * this.number; // } if (this.number % 5 == 0) { //System.out.println("---------------"); } System.out.println("finished: " + number); } public void shutDown() { if (this.proc != null) { this.proc.destroy(); } } } }