0

I got a List X from DB by invoking some methods. Now I split the List in to two separate Lists A & B by some criteria.

The two list has to be processed in different manner. But i want the to start the processing of both list at time. Don't want to wait and start processing second one.

Please advice what is the best way to do this.

Mine is spring web application. This is only to a specific service.

Thanks in Advance

Shak
  • 177
  • 1
  • 3
  • 18

3 Answers3

1

Your question is too vague. A generic answer would be to spawn a Thread for each list and process them.

(Not tested but should work fine)

class ListAProcessor implements Runnable {

    private final List<YourListClass> list;

    public ListAProcessor(final List<YourListClass> yourListA) {
        this.list = yourList;
    }

    @Override
    public void run() {
        // Process this.list
    }

}

class ListBProcessor implements Runnable {

    private final List<YourListClass> list;

    public ListBProcessor(final List<YourListClass> yourListB) {
        this.list = yourList;
    }

    @Override
    public void run() {
        // Process this.list
    }
}

public class Main {
    public static void main(final String args[]) {
        List<YourListClass> listA;
        List<YourListClass> listB;
        // Initialize the lists
        Runnable processor = new ListAProcessor(listA);
        processor.start();
        processor = new ListBProcessor(listB);
        processor.start();
    }
}
m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • 2
    If is to vague and you do not have answer then write a comment or vote to close. – Damian Leszczyński - Vash Jul 03 '13 at 08:58
  • In my service layer i got a List. I divided the List in to two by having some criteria for example having com_id and not having. Now i want to process the both List simultaneously. Normally it is I will call a function with first list and then another function with second list. Here I want to run both this function as it takes some time to finish. – Shak Jul 03 '13 at 09:00
  • Well that's the same you said in the question. If that's all, I already included an example code on how to do this. – m0skit0 Jul 03 '13 at 09:04
  • @Vash It's vague, but I have an answer :) – m0skit0 Jul 03 '13 at 09:54
0

To work more then one thread, your List should be synchronized. See below code to split List between sublist in synchronized block as well as thread safe.

    List xList= Collections.synchronizedList(new ArrayList(10));

     synchronized(xList){
     List aList=xList.subList(0, 5);
     List bList=xList.subList(5, 10);
     }
Masudul
  • 21,823
  • 5
  • 43
  • 58
  • He has 2 different lists to be processed separately. – m0skit0 Jul 03 '13 at 09:05
  • No. You abuse the synchronized key word here. The synchronizedList already have implemented synchronization there is no need to additionally synchronized them. – Damian Leszczyński - Vash Jul 03 '13 at 09:06
  • Yes...I have two lists to be processed seperatly.... The reason is it takes some time to process the first list. SO Instead of waiting, We need to process the other list also prellaly – Shak Jul 03 '13 at 09:17
0

To be able to process something as asynchronous element of program you must start new Thread for that operation. In Java exists special API, that support this type of operations.

You will need to use class Thread and interface Runnable.

{ //Body of some method

     List<Object> sourceList = getList();

     final List<Object> firstList  = createFirstList(sourceList);
     final List<Object> secondList = createsecondList(sourceList);

     //Define the Runnable, that will store the logic to process the lists.

     Runnable processFirstList = new Runnable() {//We create anonymous class
         @Override
        public void run() {
              //Here you implement the logic to process firstList
       }

     };


     Runnable processSecondList = new Runnable() {//We create anonymous class
        @Override
        public void run() {
              //Here you implement the logic to process secondList
       }

     };

     //Declare the Threads that will be responsible for execution of runable logic

     Thread firstListThread  = new Thread(processFirstList);
     Thread secondListThread = new Thread(processSecondList);

     //Start the Threads 

     firstListThread.start();
     secondListThread.start();

}

You should read the tutorial