?
Solved

Convert code to Multi threading

Posted on 2007-10-20
6
Medium Priority
?
398 Views
Last Modified: 2013-12-29
Hi -

The below code published some content into the database. I need to turn this code into a mutithreaded code so that the operation happens fastly. And the reason i need to have this is because my lineid[] contains more than 5000+ values.

public class PublishPropertyObjects {
             public static void main(String[] args) {
              
              String lineId[]={"4983462","1232134234","1234214","1234214","12342134"};             
              
              String localeCd = "de-DE";
                System.out.println("Length : "+lineId.length);
          for(int i = 0 ; i < lineId.length ;i++){
          try {
                                  
                      ServiceInvocation serviceInv = ServiceInvocation.getInstance();
                      PDNAGlobalLinkPort proxy = (PDNAGlobalLinkPort)serviceInv.create(PDNAGlobalLinkPort.class);
               
                      ApproveTranslationRequestDTO reqDTO = new ApproveTranslationRequestDTO();
                      reqDTO.setRequestContextBean(MercuryUtil.buildRequestContext());
                reqDTO.setItemId(new Long(lineId[i]).longValue());
                reqDTO.setLocalCode(localeCd);
                boolean success = proxy.approveTranslation(reqDTO);
               
                if(success)
                      System.out.println(i+"  "+lineId[i] + " " + localeCd +" -->>> approved");
                else
                      System.out.println(lineId[i] + " " + localeCd +" failed to approve");
                      
                }catch (Exception e) {
                      System.out.println("Exception -->> "+e.getMessage()+ ">> has been generated");
                      
                }
                }
          
                System.out.println(">>>>>>>>>>>>DONE<<<<<<<<<<<<<<<");
                
                
          }
    }


Is this possible?

Thank you
0
Comment
Question by:jagadeesh_motamarri
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20114919
What makes you think that multi-threading will make it faster? It's more likely to slow it down
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 20114973
my code was simply calling the service by sending the lineids one after the other. Imagine if i have 5000 records....while the service is executing one lineid why shouldn't i call the same service again and for a second one....becoz my request is independent of the responses.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20114993
You could give it a try but it's not likely to speed things up
0
Technology Partners: 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!

 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 20115005
could you please explain why?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20115050
Let's turn that around first shall we - can you say why you think it will speed things up?
0
 
LVL 13

Accepted Solution

by:
Bart Cremers earned 1500 total points
ID: 20115863
Using the classes available in the java.util.concurrent package it's pretty easy to write multithreaded code. If it's going to be faster depends on how long this client code is waiting for a response. You'll need to test if it's going to be faster.

public class PublishPropertyObjects {
    public static void main(String[] args) throws Exception {
        String lineId[] = {"4983462", "1232134234", "1234214", "1234214", "12342134"};
        String localeCd = "de-DE";
        System.out.println("Length : " + lineId.length);

        ExecutorService pool = Executors.newCachedThreadPool(10);

        for (String aLineId : lineId) {
            pool.execute(new Handler(Long.parseLong(aLineId), localeCd));
        }

        pool.shutdown();

        System.out.println(">>>>>>>>>>>>DONE<<<<<<<<<<<<<<<");
    }
}

class Handler implements Runnable {
    private final long itemId;
    private final String localeCd;

    public Handler(long itemId, String localeCd) {
        this.itemId = itemId;
        this.localeCd = localeCd;
    }

    public void run() {
        try {

            ServiceInvocation serviceInv = ServiceInvocation.getInstance();
            PDNAGlobalLinkPort proxy = (PDNAGlobalLinkPort) serviceInv.create(PDNAGlobalLinkPort.class);

            ApproveTranslationRequestDTO reqDTO = new ApproveTranslationRequestDTO();
            reqDTO.setRequestContextBean(MercuryUtil.buildRequestContext());
            reqDTO.setItemId(itemId);
            reqDTO.setLocalCode(localeCd);
            boolean success = proxy.approveTranslation(reqDTO);

            if (success) {
                System.out.println(itemId + " " + localeCd + " -->>> approved");
            } else {
                System.out.println(itemId + " " + localeCd + " failed to approve");
            }

        } catch (Exception e) {
            System.out.println("Exception -->> " + e.getMessage() + ">> has been generated");

        }
    }
}
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:
Suggested Courses
Course of the Month17 days, 7 hours left to enroll

864 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