Solved

Java threading

Posted on 2014-02-24
10
298 Views
Last Modified: 2014-02-25
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.
0
Comment
Question by:nzdjava
  • 6
  • 4
10 Comments
 
LVL 26

Expert Comment

by:dpearson
ID: 39883814
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
0
 

Author Comment

by:nzdjava
ID: 39884619
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

}
0
 

Author Comment

by:nzdjava
ID: 39884647
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

}
0
 

Author Comment

by:nzdjava
ID: 39884688
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);

}
0
 
LVL 26

Expert Comment

by:dpearson
ID: 39884770
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:nzdjava
ID: 39886815
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
0
 
LVL 26

Accepted Solution

by:
dpearson earned 500 total points
ID: 39886856
You don't want to create the executor inside the loop:

 executor = Executors.newFixedThreadPool(2) ;

This should be at the top.  An executor is a pool of threads available to do work for you.  So you can just create one and keep it around forever.

Also the awaitTermination should be after the shutdown.

You have this comment:

                              //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.

The outputs should be jumbled if you are executing the code in parallel.  One task does something, prints out some stuff, then another task does a piece of work, prints out more stuff, then the first task does some more etc.

That's the nature of parallel execution.

The code you have right now is actually still single threaded.  Because each time you submit a task (calling executor.executor(Runnable())) you then immediately wait for everything to terminate.  So you do one task, then wait for it, then do another task.  It's nice and orderly, but single threaded.

So you're close but I don't think this is quite right.

Doug
0
 

Author Comment

by:nzdjava
ID: 39886944
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";
}
0
 

Author Closing Comment

by:nzdjava
ID: 39886948
Very quick and very detailed and worked as expected
0
 
LVL 26

Expert Comment

by:dpearson
ID: 39886983
Yeah that looks pretty good.

Doug
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now