Skip to content

Instantly share code, notes, and snippets.

@irfani
Created May 13, 2012 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irfani/2689054 to your computer and use it in GitHub Desktop.
Save irfani/2689054 to your computer and use it in GitHub Desktop.
This sample demonstates the use of the new Java concurrency API
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
public class CurrencyConverter {
final String[] CURRENCY = new String[] { "IDR", "MYR", "AUD", "SGD", "THB" };
void checkRateSequential() throws Exception {
System.out.println("checkRateSequential :");
long start = System.nanoTime();
for (String ccy : CURRENCY) {
HashMap ccyRate = getRates(ccy);
System.out.println("Value of $1 in " + ccy + " is " + ccyRate.get("RATE"));
}
long end = System.nanoTime();
System.out.println("Time (seconds) taken " + (end - start) / 1.0e9);
}
void checkRateAllAtEnd() throws Exception {
System.out.println("checkRateAllAtEnd :");
long start = System.nanoTime();
List<Callable<HashMap>> tasks = new ArrayList<Callable<HashMap>>();
for (final String ccy : CURRENCY) {
tasks.add(new Callable<HashMap>() {
public HashMap call() throws Exception {
return getRates(ccy);
}
});
}
ExecutorService executorPool = Executors.newCachedThreadPool();
final List<Future<HashMap>> listRates = executorPool.invokeAll(tasks, 3600, TimeUnit.SECONDS);
for (Future<HashMap> rate : listRates) {
HashMap ccyRate = rate.get();
System.out.println("Value of $1 in " + ccyRate.get("CCY") + " is " + ccyRate.get("RATE"));
}
executorPool.shutdown();
long end = System.nanoTime();
System.out.println("Time (seconds) taken " + (end - start) / 1.0e9);
}
void checkRateAsItComplete() throws Exception {
System.out.println("checkRateAsItComplete :");
long start = System.nanoTime();
ExecutorService executorPool = Executors.newCachedThreadPool();
CompletionService<HashMap> compService = new ExecutorCompletionService<HashMap>(executorPool);
for (final String ccy : CURRENCY) {
compService.submit(new Callable<HashMap>() {
public HashMap call() throws Exception {
return getRates(ccy);
}
});
}
for (int i=0; i<CURRENCY.length; i++) {
Future<HashMap> future = compService.take();
HashMap ccyRate = future.get();
System.out.println("Value of $1 in " + ccyRate.get("CCY") + " is " + ccyRate.get("RATE"));
}
executorPool.shutdown();
long end = System.nanoTime();
System.out.println("Time (seconds) taken " + (end - start) / 1.0e9);
}
private HashMap getRates(String ccy) throws Exception {
URL url = new URL(
"http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=USD"
+ ccy + "=X");
BufferedReader reader = new BufferedReader(new InputStreamReader(
url.openStream()));
String data = reader.readLine();
String[] dataItems = data.split(",");
Double rate = Double.valueOf(dataItems[1]);
HashMap ccyRate = new HashMap();
ccyRate.put("CCY", ccy);
ccyRate.put("RATE", rate);
return ccyRate;
}
public static void main(String[] args) {
CurrencyConverter ccyConverter = new CurrencyConverter();
try {
ccyConverter.checkRateSequential();
ccyConverter.checkRateAllAtEnd();
ccyConverter.checkRateAsItComplete();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment