Solved

Java threading

Posted on 2014-02-24
10
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 28

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 28

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 28

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 28

Expert Comment

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

Doug
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

632 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