Link to home
Start Free TrialLog in
Avatar of nzdjava
nzdjava

asked on

Java threading

Hi All - I am new to java and working on Threading.

I have a List and while iterating I need to do multiple things(get data from database,insert data into database,)using threads.

Ex:

for(int i=0;i<list.size();i++){

String xyz = list.getXyg();

// using this xyz need to select data from database
//I need to insert the data into Database from the result of select statement
}


I am able to get data and insert with out threads. but the new requirement is to insert parallely and for any reason it fails I need to try two times.  And when I am done all the data in the array list then I need to go to JSP

Any tips and help should be appreciated.
Avatar of dpearson
dpearson

What you should look at is the ExecutorService:
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html

The basic structure you need is:

a) Create an executor
b) Walk the list
c) Submit a new task to the executor for each item in the list
    Each task should try to insert two times to the db
d) Call awaitTermination() on the executor - which waits for all of the tasks that are executing in parallel to terminate.

Then you can select exactly how many threads you wish to use for this by how you create the executor.  E.g.
ExecutorService executor = Executors.newFixedThreadPool(20) ;
will give you 20 threads to do the work.

See how far that takes you and then post questions if you need help with specific steps.

Doug
Avatar of nzdjava

ASKER

Doug - Can you be little detail on how to start the threads to finish the  tasks from 1 to 4

for(int I=0;i<list.size();i++){

//I need to run all these  tasks in one thread(from  1 to 4)  until the last object in the list.

1 task 1    //this goes to database and bring data
2 task2    //this has some business logic
3 task3    // inserts data into databse - This task takes more time
4 task4    //deletes data in a different database

}
Avatar of nzdjava

ASKER

for(int I=0;i<list.size();i++){

xygObject xyg = list.get(i);

// I basically use the data to run my  tasks from xygObject
//I need to run all these  tasks in one thread(from  1 to 4)  until the last object in the list.

1 task 1    //this goes to database and bring data
2 task2    //this has some business logic
3 task3    // inserts data into databse - This task takes more time
4 task4    //deletes data in a different database

}
Avatar of nzdjava

ASKER

for(int I=0;i<list.size();i++){

xygObject xyg = list.get(i);
//I  made all  tasks into one Method. Now I want to run this method paralley using threads.
Boolean success = OneMethod(xyg);

}
It should look something like this:

ExecutorService executor = Executors.newFixedThreadPool(20) ;

for (int i = 0 ; i < list.size() ; i++) {
     // You need to make this final so you can pass it into the Runnable() task below.
     final xyzObject xyz = list.get(i) ;

    // Submit a task to be executed by a thread
    // It won't run instantly, it's put on a queue and run as threads become available
    // to do the work.
     executor.execute(new Runnable() {
         public void run() {
	      boolean success = oneMethod(xyz) ;	
		}
	});
}

// Stop taking new tasks
executor.shutdown() ;

// Wait for up to a minute for all of the parallel tasks to complete
executor.awaitTermination(60, TimeUnit.SECONDS) ;

Open in new window


Hope that makes sense,

Doug
Avatar of nzdjava

ASKER

Thanks Doug for you steps. it helped me alot; I am able to start my work.


Here is the sample code and please correct me if I am doing it  right;
//method
       ExecutorService executor = null;
             
            if(newList != null && newList.size()>0){
                  try {
                        for(int i = 1 ;i<newList.size();i++){
                              count++;
                              final int xyz = i;
                              final DisplayObject dto = (DisplayObject)newList.get(i);
                            executor = Executors.newFixedThreadPool(2) ;
                              
                              //System.out.println("executor :"+executor.);
                              executor.execute(new Runnable() {
                                 public void run() {
                                      
                                       boolean success = forwardAll(xyz,dto);      
                                      
                                    }
                              });
                              //if I keep this here I am able to print every thing one after the //ohere but if I move it from here my Sys outs are getting jumbled from forwardAll //method.

                              if (executor.awaitTermination(2, TimeUnit.SECONDS)) {
                                    System.out.println("In Await");
                              }
                              
                        }
                        
                        executor.shutdown();
                        
                        // exec
                        System.out.println("Final task : " + count);
                  } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                  }catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                  }
            }


Am am I doing it in the right way. In real world app and in production it may take two to 3 minutes.
Thanks for your help
ASKER CERTIFIED SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nzdjava

ASKER

Thanks Doug. Thanks for your guidance. This time I think I got it right

public String save() {                        
             
            if(newList != null && newList.size()>0){
                  try {
                        ExecutorService executor = Executors.newFixedThreadPool(3) ;
                        for(int i = 1 ;i<newList.size();i++){
                              count++;
                              final int xyz = i;
                              final DisplayObject dto = (DisplayObject)newList.get(i);
                              executor.execute(new Runnable() {
                                    
                                 public void run() {
                                      
                                       boolean success = forwardAll(xyz,dto);      
                                      
                                    }
                              });
                        }                        
                        executor.shutdown();

                        if (executor.awaitTermination(60, TimeUnit.SECONDS)) {
                              System.out.println("All threads done with their jobs");
                        }
                        System.out.println("Final task : " + count);

                  } catch (InterruptedException e) {
                        e.printStackTrace();
                  }catch (Exception e) {
                        e.printStackTrace();
                  }
            }else{
                  System.out.println("No items to process");
            }
            
   
        return "testIteratorSuccess";
}
Avatar of nzdjava

ASKER

Very quick and very detailed and worked as expected
Yeah that looks pretty good.

Doug