首先给到需要获取和计算的股票,存入一个文本中,如stocks.txt
然后定义个抽象类,用于读取这个stocks.txt中存放的股票信息,并计算出股票的总值和读取花费的时间。代码如下:
01 |
public abstract class AbstractNAV {
|
02 |
public static Map<String, Integer> readTickers() throws IOException {
|
03 |
final BufferedReader reader =
|
04 |
new BufferedReader( new FileReader( "d:/stocks.txt" ));
|
06 |
final Map<String, Integer> stocks = new HashMap<String, Integer>();
|
08 |
String stockInfo = null ;
|
09 |
while ((stockInfo = reader.readLine()) != null ) {
|
10 |
final String[] stockInfoData = stockInfo.split( "," );
|
11 |
final String stockTicker = stockInfoData[ 0 ];
|
12 |
final Integer quantity = Integer.valueOf(stockInfoData[ 1 ]);
|
14 |
stocks.put(stockTicker, quantity);
|
20 |
public void timeAndComputeValue()
|
21 |
throws ExecutionException, InterruptedException, IOException {
|
22 |
final long start = System.nanoTime();
|
24 |
final Map<String, Integer> stocks = readTickers();
|
25 |
final double nav = computeNetAssetValue(stocks);
|
27 |
final long end = System.nanoTime();
|
29 |
final String value = new DecimalFormat( "$##,##0.00" ).format(nav);
|
30 |
System.out.println( "Your net asset value is " + value);
|
31 |
System.out.println( "Time (seconds) taken " + (end - start)/ 1 .0e9);
|
34 |
public abstract double computeNetAssetValue(
|
35 |
final Map<String, Integer> stocks)
|
36 |
throws ExecutionException, InterruptedException, IOException;
|
然后,我们用传统的单线程方式,依次去读取并计算股票,并打印总价和花费的时间,代码如下:
01 |
public class SequentialNAV extends AbstractNAV {
|
02 |
public double computeNetAssetValue(
|
03 |
final Map<String, Integer> stocks) throws IOException {
|
04 |
double netAssetValue = 0.0 ;
|
05 |
for (String ticker : stocks.keySet()) {
|
06 |
netAssetValue += stocks.get(ticker) * YahooFinance.getPrice(ticker);
|
11 |
public static void main( final String[] args)
|
12 |
throws ExecutionException, IOException, InterruptedException {
|
13 |
new SequentialNAV().timeAndComputeValue();
|
由于网络问题,我这里运行之后,得到的结果是:
1 |
Your net asset value is $ 18 , 317 , 338.21
|
2 |
Time (seconds) taken 18.080151543
|
紧接着,我们用多线程方式,读取并计算,并打印总价和时间花费
01 |
public class ConcurrentNAV extends AbstractNAV {
|
02 |
public double computeNetAssetValue( final Map<String, Integer> stocks)
|
03 |
throws InterruptedException, ExecutionException {
|
04 |
final int numberOfCores = Runtime.getRuntime().availableProcessors();
|
05 |
final double blockingCoefficient = 0.9 ;
|
06 |
final int poolSize = ( int )(numberOfCores / ( 1 - blockingCoefficient));
|
08 |
System.out.println( "Number of Cores available is " + numberOfCores);
|
09 |
System.out.println( "Pool size is " + poolSize);
|
12 |
final List<Callable<Double>> partitions =
|
13 |
new ArrayList<Callable<Double>>();
|
14 |
for ( final String ticker : stocks.keySet()) {
|
15 |
partitions.add( new Callable<Double>() {
|
16 |
public Double call() throws Exception {
|
17 |
return stocks.get(ticker) * YahooFinance.getPrice(ticker);
|
23 |
final ExecutorService executorPool =
|
24 |
Executors.newFixedThreadPool(poolSize);
|
27 |
final List<Future<Double>> valueOfStocks =
|
28 |
executorPool.invokeAll(partitions, 10000 , TimeUnit.SECONDS);
|
30 |
double netAssetValue = 0.0 ;
|
31 |
for ( final Future<Double> valueOfAStock : valueOfStocks)
|
32 |
netAssetValue += valueOfAStock.get();
|
34 |
executorPool.shutdown();
|
38 |
public static void main( final String[] args)
|
39 |
throws ExecutionException, InterruptedException, IOException {
|
40 |
new ConcurrentNAV().timeAndComputeValue();
|
在跟上面同等的网络环境下,这段代码运行之后的结果为:
1 |
Number of Cores available is 4
|
3 |
Your net asset value is $ 18 , 317 , 338.21
|
4 |
Time (seconds) taken 0.715660335
|
多线程获取yahoo股票信息,布布扣,bubuko.com
多线程获取yahoo股票信息