5 Replies Latest reply: Jan 23, 2008 8:00 AM by 807601 RSS

    Creating a Thread so that I don't wait on a Method?

    807601
      Good Evening All!

      I have a simple java program that reads a file containing XML and then posts that XML to a webserver. When I run the program I would normally have about 5,000 xml "documents" in the file that need to be posted to the webserver. Today, I have create a method to read from the file and then a method to post the data and that is contained in a do loop. The issue that I have is that it takes sever seconds for the webserver to respond to each request. Thus it takes about 5 seconds for each call to post. (The Post method that I have created does not return any data. It does connect to a logging database and logs the result that it received. )

      Instead of waiting for the call to Post to come back, could I create a thread and then run post inside of its own thread so that I can read the next line in the file and post it before the first finishes?

      Thanks,
      Doug
        • 1. Re: Creating a Thread so that I don't wait on a Method?
          807601
          Sure, but you should cap the number of simultaneous requests. I think a producer-consumer pattern might work here: the thread reading the XML file produces work, which is given to an idle consumer from a pool of consumers, which performs the actual submission to the server.
          • 2. Re: Creating a Thread so that I don't wait on a Method?
            807601
            Thanks Paul.

            I will search for some examples of that, but if you know of any good ones I would love to have them (or links to them).

            Doug
            • 3. Re: Creating a Thread so that I don't wait on a Method?
              807601
              DougJrS wrote:
              I will search for some examples of that, but if you know of any good ones I would love to have them (or links to them).
              Consider using an ExecutorService from java.util.concurrent.Executors, which can help manage a thread pool. Depending on the size of the work you read, a couple possible approaches are:

              1. Synchronous hand-off, where the work is handed off directly to a worker. This means that if no workers are free, the producer (xml reader) thread will block until one is free.
              2. Asynchronous (queue), where work is simply placed in a queue, and when workers finish a job, they go back to the queue and grab the next available job. The queue can be bounded or unbounded, where a bounded queue limits memory consumption but the producer thread may block until space is freed in the queue.
              • 4. Re: Creating a Thread so that I don't wait on a Method?
                807601
                Thanks Again Paul.

                If I can ask one more...How do you pass a recordSet to run? I may have over stated how simple my program is. The code is below. It is reading an exce file and then passing two of the cell values to my post method.

                I have tried a couple of things, but so far no matter what I do I can't put the rs.getString() calls in run without the Microsoft driver producing errors.

                So, in this case do you have any advice?

                Thanks,
                Doug


                public static void main(String[] args)
                {
                Connection c = null;
                Statement stmnt = null;
                try
                {
                Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
                c = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=" + args[0]);
                stmnt = c.createStatement();
                String query = "Select * from [data$]" ;
                ResultSet rs = stmnt.executeQuery( query );
                
                while( rs.next() )
                {
                XML.Post( rs.getString(1) + rs.getString(2) );
                }
                }
                catch( Exception e )
                {
                System.err.println( e );
                }
                
                
                }
                • 5. Re: Creating a Thread so that I don't wait on a Method?
                  807601
                  DougJrS wrote:
                  Thanks Again Paul.

                  If I can ask one more...How do you pass a recordSet to run? I may have over stated how simple my program is. The code is below. It is reading an exce file and then passing two of the cell values to my post method.

                  I have tried a couple of things, but so far no matter what I do I can't put the rs.getString() calls in run without the Microsoft driver producing errors.
                  Create a Record object that holds all the data needed to do the POST.