This discussion is archived
4 Replies Latest reply: Nov 15, 2012 5:07 AM by jtahlborn RSS

How to write a multithreaded program

971973 Newbie
Currently Being Moderated
I have a utility program which populates data from table1 to table2 after doing some validation on each row. In table1 i have multiple rows for the same account(Lets say 1 account will have 10 rows,like that i will have many accounts).First I will be fetching all the distinct account from the table1 and then I need to write a mutlithreaded program which can run this utility program concurrently for all the accounts. I am confused how exactly how should i write this program?

I am thinking to use ThreadPoolExecutor and will create specific task for my utility program. But i am confused that should i first create a list of all my task having my account and invoke the method invokeAll(taskList). And in invokeAll Should I loop through all my accounts and invoke the run method on the Callable task.

Also, i want to know that how can i find out that actually my program is multithreaded and its behaving in the same manner?

Please help me on this?

Let me know if you require any more information.
  • 1. Re: How to write a multithreaded program
    jtahlborn Expert
    Currently Being Moderated
    968970 wrote:
    I am thinking to use ThreadPoolExecutor and will create specific task for my utility program. But i am confused that should i first create a list of all my task having my account and invoke the method invokeAll(taskList). And in invokeAll Should I loop through all my accounts and invoke the run method on the Callable task.
    you don't implement invokeAll(), ThreadPoolExecutor does. it is just a shorthand method for submitting all your tasks if you already have them in a Collection.

    however, that's most likely not how you want to do it. you should submit each account task as you create it so that some of the tasks can be running while you are still finding more accounts.
    Also, i want to know that how can i find out that actually my program is multithreaded and its behaving in the same manner?
    if you want to go low tech, you can print out progress info from each task and see that they are running in parallel. or, you could attach a profiler to the java process and watch the threads in action.

    i would also suggest reading some of the many excellent multi-threading tutorials out there to make sure you are good on the basics.
  • 2. Re: How to write a multithreaded program
    971973 Newbie
    Currently Being Moderated
    I am having lacks of account and so reading the account one by one will give me a performance hit. So I am not getting how to go about it. Also, i am not getting how can i do connection pooling in a standalone program. I am not gonna deploy this on any server.It will run as a batch process.Please help me.
  • 3. Re: How to write a multithreaded program
    971973 Newbie
    Currently Being Moderated
    Can any one help on this?
  • 4. Re: How to write a multithreaded program
    jtahlborn Expert
    Currently Being Moderated
    968970 wrote:
    Can any one help on this?
    If you want more help i would suggest ask specific questions, and including code is a good thing, too.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points