Solved

Java threading

Posted on 2014-02-24
10
301 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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
 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

803 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