-2

I have a list with say 1000 entries.

public static List<Map<String,Object>> stockSymbolsListMap = new ArrayList<Map<String,Object>>();

This list contains stockSymbols that I need to use to pull data from an exchange website. Now to iterate over it and do it one by one is a slow process. i want to break this list in say pieces of 100 and spawn a separate thread for each part to fetch the results. Also this process will be repeated every second.

How do I achieve this?

Sambhav Sharma
  • 5,741
  • 9
  • 53
  • 95
  • 1
    Try attempting to write a multithread code using java Thread. And post the issues you come across. Please find multi thread tutorial [here](http://www.tutorialspoint.com/java/java_multithreading.htm) or you can browse for good multi thread tutorials – Murthy Apr 17 '14 at 14:27
  • My main question is how to work out the list part? What would be the better to split the index in equal size and use them. For multithreading I am using Java's executor service with fixed thread pool. – Sambhav Sharma Apr 17 '14 at 14:29
  • You may find using a `ForkJoinPool` here a good fit. There's a great article on it [here](http://www.javaspecialists.eu/archive/Issue201.html). – OldCurmudgeon Apr 17 '14 at 14:30

3 Answers3

1

Could use Guava's List.partition to split the list into blocks to process in each thread.

John B
  • 32,493
  • 6
  • 77
  • 98
1

Use java's completion service with a fixed thread pool and simply submit a callable for every element in the list. You can control the number which happen at once by changing the size of the thread pool.

The completion service acts like a queue/blocking queue, so you can poll the answers as they are ready.

phil_20686
  • 4,000
  • 21
  • 38
1

Here is another solution that does not use any external resources aside from java.util

Instead of using

public static List<Map<String,Object>> stockSymbolsListMap = new ArrayList<Map<String,Object>>();

I used

private static ArrayList<bleh> eh = new ArrayList<bleh>();

Step 1. Create array

    for(int i = 0; i < 450; i++){
        eh.add(new bleh());
    }

Step 2. Partition the list (Did not spend much time on this, so the code is a tad messy) Let BLOCK_SIZE = the size of each parition

    int listAmount;
    if(eh.size()%BLOCK_SIZE != 0)
        listAmount = eh.size()/BLOCK_SIZE + 1;
    else
        listAmount = eh.size()/BLOCK_SIZE;

    List<bleh>[] lists = new List[listAmount];

    for(int i = 1; i <= listAmount; i++){
        if(i * BLOCK_SIZE < eh.size()){
            lists[i - 1] = eh.subList( (i - 1) * BLOCK_SIZE, eh.size());
        }
        else{
            lists[i - 1] = eh.subList( (i - 1) * BLOCK_SIZE, i * BLOCK_SIZE);
        }
    }

Step 3. Run the lists concurrently

    ExecutorService executor = Executors.newCachedThreadPool();

    for(List<bleh> list : lists){
        executor.execute(new MyThread(list));
    }

The MyThread class I used is as such

private static class MyThread implements Runnable{
    private List<bleh> eh = null;

    public MyThread(List<bleh> list){
        eh = list;
    }

    @Override
    public void run(){
        System.out.println("SOME THREAD");
        for(bleh meh : eh){
            System.out.print(meh.toString());
        }
    }
}

To use Executors you will need to import

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

or simply

import java.util.concurrent.*;

UPDATE You may want to also scale the amount of threads created in accordance to the amount of threads available in which the following link should be of use, How to Scale Threads According to CPU Cores.

Community
  • 1
  • 1
Sahar Rabinoviz
  • 1,939
  • 2
  • 17
  • 28