Part 7: java.util.concurrent - invokeAll via ExecutorService(二)

2014-11-24 09:04:26 · 作者: · 浏览: 1
rue;
} catch (Exception e) {
return false;
}
}
}

// MAIN THREAD
public class InvokeAllExample {

private static final ExecutorService executorPool = Executors.newFixedThreadPool(20);
private static final int NO_OF_CLIENT = 1000;
private static final String FILE_EXT = ".txt";
private static String TXN_DATA = "SOME RANDOM TXN DATA FOR CLIENT --> ";
private static String DIRECTORY = "EXAMPLE" + File.separator;

static {
if (!new File(DIRECTORY).isDirectory()) {
new File(DIRECTORY).mkdir();
}
}

public static void main(String[] args) {
int success = 0;
int failure = 0;
/**
* Lets assume we have 1000 clients connected and sending request at a
* time.
*/
Collection collection = new ArrayList();
for (int i = 0; i < NO_OF_CLIENT; i++) {
FileTask task = new FileTask(DIRECTORY + Integer.toString(i) + FILE_EXT, TXN_DATA + i);
collection.add(task);
}
long startTime = new Date().getTime();
try {
List> list = executorPool.invokeAll(collection);
for (Future fut : list) {
int ignore = fut.get() success++ : failure++;
}

} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("TOTAL SUCCESS - " + success);
System.out.println("TOTAL FAILURE - " + failure);
System.out.println("Total time - " + (new Date().getTime() - startTime) + " ms");
executorPool.shutdown();
}
} // End of Main

}


作者:沉默是金