Solved

Java threading

Posted on 2014-02-24
10
297 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:nzdjava
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Very quick and very detailed and worked as expected
0
 
LVL 26

Expert Comment

by:dpearson
Comment Utility
Yeah that looks pretty good.

Doug
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

744 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

17 Experts available now in Live!

Get 1:1 Help Now