0

I am trying to create a threadPool from a collections of objects. There are 1000 objects in the collections. Each objects calls a function to a get a string list and store them in a Output String list.

But the problem here is I have to spawn around 1000 threads to fetch the list simultaneously or I have to sequentially iterate through the list and fetch the list of String list.

So what I am trying to do here, instead of having only 1 object per thread. I am having a collection of X objects per thread(10 objects per thread). Then loop through the objects moving each one.

Is there any way instead of looping through the objects to directly create a threadpool or a executor service?

Or Is there any other way of working this problem around?

                           ----   main thread   -----
     (thread1)            |     (thread2)|   (thread3)|
                       group1         group2       group3
     (Subthreads)       |   |          |   |         |  |
                      obj1 obj2      obj3 obj    obj5 obj6

public class ExampleApplication {

static List<String> outputList = new ArrayList<String>();

static ExecutorService executor = Executors.newScheduledThreadPool(10);

public static void main(String[] args) throws InterruptedException {

    List<Customer> list = new ArrayList<Customer>();

    //Example list of Object Collection
    for (int i = 0; i < 100; i++) {
        list.add(new Customer());
    }

    executor.execute(new Query(list.subList(0, 25)));
    executor.execute(new Query(list.subList(26, 50)));
    executor.execute(new Query(list.subList(51, 75)));
    executor.execute(new Query(list.subList(76, 100)));

    executor.shutdown();
}

static class Query implements Runnable {

    List<Customer> list;

    public Query(List<Customer> list) {
        // TODO Auto-generated constructor stub
        this.list = list;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        for (Customer customer : list) {
            //Adds a list of String to this output list
            //customer.rec() returns a list of String
            outputList.addAll(customer.rec());
        }
    }
}
}

Above is an example problem. Here I created a threadpool of 10, In the Query class, I am kind of iterating the list of objects to fetch the String list. So Instead of iterating the collection of object, is there any way to make it to collection of X objects per thread(10 objects per thread). Is there any example I can refer to?

user2109988
  • 47
  • 3
  • 10
  • Sorry, you haven't provided enough detail for us to understand the problem. – Jim Garrison Mar 04 '16 at 19:24
  • and note that too many thread will slow your program. – Y.Kaan Yılmaz Mar 04 '16 at 19:26
  • 2
    Why not using the same pool for each collection ? The size of the pool would be based on the size of the collection, with a maximum ? – Benoit Vanalderweireldt Mar 04 '16 at 19:27
  • Your main() creates a thread pool, it submits _one_ task to the pool, and then it exits. That doesn't seem very useful. You might just as well have the main() perform the one task itself. If you want more than one thread to run at the same time, then you're going to have to call `executor.execute(...)` more than once. – Solomon Slow Mar 04 '16 at 19:54
  • You're creating a `ScheduledExecutorService` but you're not using it to _schedule_ anything (i.e., you are not calling any of its `schedule(task, delay, timeunit)` methods. I suppose that _works_ but it's a non-obvious thing you're doing. – Solomon Slow Mar 04 '16 at 19:58
  • You can split your list and process each list in a thread. More info on split list: http://stackoverflow.com/questions/2895342/java-how-can-i-split-an-arraylist-in-multiple-small-arraylists – Rodrigo Menezes Mar 04 '16 at 19:59
  • Speaking of the non-obvious: You have declared a class named `ThreadPool`, but it is _not_ a thread pool: It _uses_ a thread pool. The name of a class should say what it _is_ (e.g., `MainClass`, or `MyFoobarApplication` or, ...) – Solomon Slow Mar 04 '16 at 19:59
  • Thanks everyone, based on your thoughts I have updated my code. – user2109988 Mar 04 '16 at 20:42

0 Answers0