Thread Exercise for the gurus

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alvin Bruney

    #1

    Thread Exercise for the gurus

    I have an array list of queries. The arraylist is variable, anywhere from 10
    to 10000 or more. I'd like to spin threads to take chunks of 500 queries out
    of that array list, no more than 10 threads (context switching reasons). if
    it's less than 500 i spin only one thread.

    I am having trouble building an efficient, clean implementation of this.
    Clean means, i don't want to loop from 500 to 1000 if i only have 650 items,
    i would like to stop at 650. Efficient means i don't want the code to be
    messy - like what i have now. readable. I don't need the thread code part, i
    need the looping mechanism. I have it done, but it's not clean, tight or
    efficient. I know it can be done better. Efficiency also means efficiently
    locking and releasing the datastructure.

    See if you can help, otherwise i'll run with what i have. (I can't think
    clearly now, the ax just fell here and we lost some very good folk)


  • Greg Bacchus

    #2
    Re: Thread Exercise for the gurus

    Its not clear if you're pre-assigning the queries to the threads, or the
    thread is going back to the original list each time it completes a query.

    Of course the tidiest way would be to pre-assign the queries to the thread,
    but this may not achieve what you are wanting, if say one thread works
    through its queries a lot faster than another (for some reason).

    Greg

    "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
    message news:O%23WNmj2Z DHA.4020@tk2msf tngp13.phx.gbl. ..[color=blue]
    > I have an array list of queries. The arraylist is variable, anywhere from[/color]
    10[color=blue]
    > to 10000 or more. I'd like to spin threads to take chunks of 500 queries[/color]
    out[color=blue]
    > of that array list, no more than 10 threads (context switching reasons).[/color]
    if[color=blue]
    > it's less than 500 i spin only one thread.
    >
    > I am having trouble building an efficient, clean implementation of this.
    > Clean means, i don't want to loop from 500 to 1000 if i only have 650[/color]
    items,[color=blue]
    > i would like to stop at 650. Efficient means i don't want the code to be
    > messy - like what i have now. readable. I don't need the thread code part,[/color]
    i[color=blue]
    > need the looping mechanism. I have it done, but it's not clean, tight or
    > efficient. I know it can be done better. Efficiency also means efficiently
    > locking and releasing the datastructure.
    >
    > See if you can help, otherwise i'll run with what i have. (I can't think
    > clearly now, the ax just fell here and we lost some very good folk)
    >
    >[/color]


    Comment

    • Chad Myers

      #3
      Re: Thread Exercise for the gurus


      "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote
      in message news:O%23WNmj2Z DHA.4020@tk2msf tngp13.phx.gbl. ..[color=blue]
      > I have an array list of queries. The arraylist is variable, anywhere[/color]
      from 10[color=blue]
      > to 10000 or more. I'd like to spin threads to take chunks of 500[/color]
      queries out[color=blue]
      > of that array list, no more than 10 threads (context switching[/color]
      reasons). if[color=blue]
      > it's less than 500 i spin only one thread.
      >
      > I am having trouble building an efficient, clean implementation of[/color]
      this.[color=blue]
      > Clean means, i don't want to loop from 500 to 1000 if i only have 650[/color]
      items,[color=blue]
      > i would like to stop at 650. Efficient means i don't want the code to[/color]
      be[color=blue]
      > messy - like what i have now. readable. I don't need the thread code[/color]
      part, i[color=blue]
      > need the looping mechanism. I have it done, but it's not clean, tight[/color]
      or[color=blue]
      > efficient. I know it can be done better. Efficiency also means[/color]
      efficiently[color=blue]
      > locking and releasing the datastructure.
      >
      > See if you can help, otherwise i'll run with what i have. (I can't[/color]
      think[color=blue]
      > clearly now, the ax just fell here and we lost some very good folk)[/color]


      How much performance to you hope to gain here? I mean, the processor
      can only execute so many instructions per second and splitting things
      up into multiple threads is just adding more instructions to be
      processed.

      Unless you have a UI that you need to keep active, or you're processing
      things that are asynchronous (database, file reads/writes, web service
      calls), threading isn't going to help you much.

      If this is a process of just looping through some calculations and
      doing them one-by-one, you're probably just better off doing them
      synchronously.

      -c


      Comment

      • Alvin Bruney

        #4
        Re: Thread Exercise for the gurus

        I stack an array of 2000 queries in an array list. If i don't use threads,
        it takes about 30 seconds. Boss don't like that, he wants to draw blood from
        stone. So I figure i can spin 4 threads at 500 queries each at the same time
        which would give him the performance he is looking for albeit at the cost of
        cpu i know.

        "Chad Myers" <cmyers@N0.SP.A M.austin.rr.com > wrote in message
        news:eQAV$F5ZDH A.1832@TK2MSFTN GP10.phx.gbl...[color=blue]
        >
        > "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote
        > in message news:O%23WNmj2Z DHA.4020@tk2msf tngp13.phx.gbl. ..[color=green]
        > > I have an array list of queries. The arraylist is variable, anywhere[/color]
        > from 10[color=green]
        > > to 10000 or more. I'd like to spin threads to take chunks of 500[/color]
        > queries out[color=green]
        > > of that array list, no more than 10 threads (context switching[/color]
        > reasons). if[color=green]
        > > it's less than 500 i spin only one thread.
        > >
        > > I am having trouble building an efficient, clean implementation of[/color]
        > this.[color=green]
        > > Clean means, i don't want to loop from 500 to 1000 if i only have 650[/color]
        > items,[color=green]
        > > i would like to stop at 650. Efficient means i don't want the code to[/color]
        > be[color=green]
        > > messy - like what i have now. readable. I don't need the thread code[/color]
        > part, i[color=green]
        > > need the looping mechanism. I have it done, but it's not clean, tight[/color]
        > or[color=green]
        > > efficient. I know it can be done better. Efficiency also means[/color]
        > efficiently[color=green]
        > > locking and releasing the datastructure.
        > >
        > > See if you can help, otherwise i'll run with what i have. (I can't[/color]
        > think[color=green]
        > > clearly now, the ax just fell here and we lost some very good folk)[/color]
        >
        >
        > How much performance to you hope to gain here? I mean, the processor
        > can only execute so many instructions per second and splitting things
        > up into multiple threads is just adding more instructions to be
        > processed.
        >
        > Unless you have a UI that you need to keep active, or you're processing
        > things that are asynchronous (database, file reads/writes, web service
        > calls), threading isn't going to help you much.
        >
        > If this is a process of just looping through some calculations and
        > doing them one-by-one, you're probably just better off doing them
        > synchronously.
        >
        > -c
        >
        >[/color]


        Comment

        • Alvin Bruney

          #5
          Re: Thread Exercise for the gurus

          I can't use the thread pool because i need to join and wait on all threads
          to complete.
          "Dave" <kdlevine@wi.rr .com> wrote in message
          news:Od36t34ZDH A.1832@TK2MSFTN GP10.phx.gbl...[color=blue]
          >
          > Have you considerd using the thread pool? Instead of adding the queries to
          > an array list you could you queue up a work item to the thread pool.
          >
          > Sorry to hear about the axes falling...
          >
          > "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
          > message news:O%23WNmj2Z DHA.4020@tk2msf tngp13.phx.gbl. ..[color=green]
          > > I have an array list of queries. The arraylist is variable, anywhere[/color][/color]
          from[color=blue]
          > 10[color=green]
          > > to 10000 or more. I'd like to spin threads to take chunks of 500 queries[/color]
          > out[color=green]
          > > of that array list, no more than 10 threads (context switching reasons).[/color]
          > if[color=green]
          > > it's less than 500 i spin only one thread.
          > >
          > > I am having trouble building an efficient, clean implementation of this.
          > > Clean means, i don't want to loop from 500 to 1000 if i only have 650[/color]
          > items,[color=green]
          > > i would like to stop at 650. Efficient means i don't want the code to be
          > > messy - like what i have now. readable. I don't need the thread code[/color][/color]
          part,[color=blue]
          > i[color=green]
          > > need the looping mechanism. I have it done, but it's not clean, tight or
          > > efficient. I know it can be done better. Efficiency also means[/color][/color]
          efficiently[color=blue][color=green]
          > > locking and releasing the datastructure.
          > >
          > > See if you can help, otherwise i'll run with what i have. (I can't think
          > > clearly now, the ax just fell here and we lost some very good folk)
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Alvin Bruney

            #6
            Re: Thread Exercise for the gurus

            The queries are already written and stacked in an arraylist. I just want to
            efficiently pick out blocks of 500 to submit at the same time. for ex. if i
            had 2000 records, i would spin 4 threads where each thread's work would be
            to insert 500 records. The reasoning here is 500 at the same time among four
            threads should be faster than 2000 on the main thread. We've got cpu
            resources to burn.

            i'd like to pre-assign - to answer your question. so each thread gets a
            block of 500 and works it until it is done and then waits on the other
            threads to complete.

            after that i have a stored proc thing lined up if he still aint satisfied
            with the results.

            "Greg Bacchus" <FBRAJGOPLBRF@s pammotel.com> wrote in message
            news:OiHVzx3ZDH A.2572@TK2MSFTN GP12.phx.gbl...[color=blue]
            > Its not clear if you're pre-assigning the queries to the threads, or the
            > thread is going back to the original list each time it completes a query.
            >
            > Of course the tidiest way would be to pre-assign the queries to the[/color]
            thread,[color=blue]
            > but this may not achieve what you are wanting, if say one thread works
            > through its queries a lot faster than another (for some reason).
            >
            > Greg
            >
            > "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
            > message news:O%23WNmj2Z DHA.4020@tk2msf tngp13.phx.gbl. ..[color=green]
            > > I have an array list of queries. The arraylist is variable, anywhere[/color][/color]
            from[color=blue]
            > 10[color=green]
            > > to 10000 or more. I'd like to spin threads to take chunks of 500 queries[/color]
            > out[color=green]
            > > of that array list, no more than 10 threads (context switching reasons).[/color]
            > if[color=green]
            > > it's less than 500 i spin only one thread.
            > >
            > > I am having trouble building an efficient, clean implementation of this.
            > > Clean means, i don't want to loop from 500 to 1000 if i only have 650[/color]
            > items,[color=green]
            > > i would like to stop at 650. Efficient means i don't want the code to be
            > > messy - like what i have now. readable. I don't need the thread code[/color][/color]
            part,[color=blue]
            > i[color=green]
            > > need the looping mechanism. I have it done, but it's not clean, tight or
            > > efficient. I know it can be done better. Efficiency also means[/color][/color]
            efficiently[color=blue][color=green]
            > > locking and releasing the datastructure.
            > >
            > > See if you can help, otherwise i'll run with what i have. (I can't think
            > > clearly now, the ax just fell here and we lost some very good folk)
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Chad Myers

              #7
              Re: Thread Exercise for the gurus

              Ah, ok. That's good then.

              So just create (#queries)/500 + 1 queues and loop
              through the array list and throw the various references
              into the various queues and then spawn (#queries)/500 + 1
              (or 10, which ever is less) threads and assign the
              queues to the threads.

              Each thread can open a connection and execute it's query
              and then close the connection (the connections are pooled,
              so there's no real performance hit) and then start over
              again. I don't know, I suppose you could leave the connection
              open, but when you "close" the connection, the connection
              sends a reset command to SQL which resets all data, temp tables,
              transactions, etc and makes it all nice and clean for the
              next statement. It's quite fast, actually.

              What pare are you having the most problems with?

              -c

              "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote
              in message news:%23K%233mz 5ZDHA.1280@tk2m sftngp13.phx.gb l...[color=blue]
              > I stack an array of 2000 queries in an array list. If i don't use[/color]
              threads,[color=blue]
              > it takes about 30 seconds. Boss don't like that, he wants to draw[/color]
              blood from[color=blue]
              > stone. So I figure i can spin 4 threads at 500 queries each at the[/color]
              same time[color=blue]
              > which would give him the performance he is looking for albeit at the[/color]
              cost of[color=blue]
              > cpu i know.
              >
              > "Chad Myers" <cmyers@N0.SP.A M.austin.rr.com > wrote in message
              > news:eQAV$F5ZDH A.1832@TK2MSFTN GP10.phx.gbl...[color=green]
              > >
              > > "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com>[/color][/color]
              wrote[color=blue][color=green]
              > > in message news:O%23WNmj2Z DHA.4020@tk2msf tngp13.phx.gbl. ..[color=darkred]
              > > > I have an array list of queries. The arraylist is variable,[/color][/color][/color]
              anywhere[color=blue][color=green]
              > > from 10[color=darkred]
              > > > to 10000 or more. I'd like to spin threads to take chunks of 500[/color]
              > > queries out[color=darkred]
              > > > of that array list, no more than 10 threads (context switching[/color]
              > > reasons). if[color=darkred]
              > > > it's less than 500 i spin only one thread.
              > > >
              > > > I am having trouble building an efficient, clean implementation of[/color]
              > > this.[color=darkred]
              > > > Clean means, i don't want to loop from 500 to 1000 if i only have[/color][/color][/color]
              650[color=blue][color=green]
              > > items,[color=darkred]
              > > > i would like to stop at 650. Efficient means i don't want the code[/color][/color][/color]
              to[color=blue][color=green]
              > > be[color=darkred]
              > > > messy - like what i have now. readable. I don't need the thread[/color][/color][/color]
              code[color=blue][color=green]
              > > part, i[color=darkred]
              > > > need the looping mechanism. I have it done, but it's not clean,[/color][/color][/color]
              tight[color=blue][color=green]
              > > or[color=darkred]
              > > > efficient. I know it can be done better. Efficiency also means[/color]
              > > efficiently[color=darkred]
              > > > locking and releasing the datastructure.
              > > >
              > > > See if you can help, otherwise i'll run with what i have. (I can't[/color]
              > > think[color=darkred]
              > > > clearly now, the ax just fell here and we lost some very good[/color][/color][/color]
              folk)[color=blue][color=green]
              > >
              > >
              > > How much performance to you hope to gain here? I mean, the processor
              > > can only execute so many instructions per second and splitting[/color][/color]
              things[color=blue][color=green]
              > > up into multiple threads is just adding more instructions to be
              > > processed.
              > >
              > > Unless you have a UI that you need to keep active, or you're[/color][/color]
              processing[color=blue][color=green]
              > > things that are asynchronous (database, file reads/writes, web[/color][/color]
              service[color=blue][color=green]
              > > calls), threading isn't going to help you much.
              > >
              > > If this is a process of just looping through some calculations and
              > > doing them one-by-one, you're probably just better off doing them
              > > synchronously.
              > >
              > > -c
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Jon Skeet

                #8
                Re: Thread Exercise for the gurus

                Alvin Bruney <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote:[color=blue]
                > The queries are already written and stacked in an arraylist. I just want to
                > efficiently pick out blocks of 500 to submit at the same time. for ex. if i
                > had 2000 records, i would spin 4 threads where each thread's work would be
                > to insert 500 records. The reasoning here is 500 at the same time among four
                > threads should be faster than 2000 on the main thread. We've got cpu
                > resources to burn.
                >
                > i'd like to pre-assign - to answer your question. so each thread gets a
                > block of 500 and works it until it is done and then waits on the other
                > threads to complete.[/color]

                One simple way would be:

                o Work out how many threads you'll need (divide by 500 and limit to 10)
                o Create a new list for each thread
                o Round-robin copy items from the old list to the new one
                o Set the threads going

                The copying shouldn't take long, and as you allocate them in a round-
                robin fashion it'll be fair however many threads you have, without
                working out any complicated boundary conditions. Once the lists are
                written out, you'll need a memory barrier on each of the threads to
                make sure that they all get all of the data, but after that there's no
                interference between them, so they can each grab items off their own
                list without worrying about the other threads.

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • Dave

                  #9
                  Re: Thread Exercise for the gurus

                  You should be able to use a semaphore to accomplish the same purpose. If you
                  know in advance how many total queries there are you can set the semaphore
                  to that value and then as each work item completes you pulse the semaphore.
                  When the last one completes the thread waiting wakes up.

                  If the number of items can change during processing then this approach needs
                  to be modified, but then, so does the other approach.

                  I mention this because the system thread pool already takes in account all
                  those factors you wanted to handle; efficient waiting and division of
                  workload, balancing the number of threads against the number of cpus,
                  autodecay and terminate, etc.

                  "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
                  message news:uogN0z5ZDH A.440@tk2msftng p13.phx.gbl...[color=blue]
                  > I can't use the thread pool because i need to join and wait on all threads
                  > to complete.
                  > "Dave" <kdlevine@wi.rr .com> wrote in message
                  > news:Od36t34ZDH A.1832@TK2MSFTN GP10.phx.gbl...[color=green]
                  > >
                  > > Have you considerd using the thread pool? Instead of adding the queries[/color][/color]
                  to[color=blue][color=green]
                  > > an array list you could you queue up a work item to the thread pool.
                  > >
                  > > Sorry to hear about the axes falling...
                  > >
                  > > "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote[/color][/color]
                  in[color=blue][color=green]
                  > > message news:O%23WNmj2Z DHA.4020@tk2msf tngp13.phx.gbl. ..[color=darkred]
                  > > > I have an array list of queries. The arraylist is variable, anywhere[/color][/color]
                  > from[color=green]
                  > > 10[color=darkred]
                  > > > to 10000 or more. I'd like to spin threads to take chunks of 500[/color][/color][/color]
                  queries[color=blue][color=green]
                  > > out[color=darkred]
                  > > > of that array list, no more than 10 threads (context switching[/color][/color][/color]
                  reasons).[color=blue][color=green]
                  > > if[color=darkred]
                  > > > it's less than 500 i spin only one thread.
                  > > >
                  > > > I am having trouble building an efficient, clean implementation of[/color][/color][/color]
                  this.[color=blue][color=green][color=darkred]
                  > > > Clean means, i don't want to loop from 500 to 1000 if i only have 650[/color]
                  > > items,[color=darkred]
                  > > > i would like to stop at 650. Efficient means i don't want the code to[/color][/color][/color]
                  be[color=blue][color=green][color=darkred]
                  > > > messy - like what i have now. readable. I don't need the thread code[/color][/color]
                  > part,[color=green]
                  > > i[color=darkred]
                  > > > need the looping mechanism. I have it done, but it's not clean, tight[/color][/color][/color]
                  or[color=blue][color=green][color=darkred]
                  > > > efficient. I know it can be done better. Efficiency also means[/color][/color]
                  > efficiently[color=green][color=darkred]
                  > > > locking and releasing the datastructure.
                  > > >
                  > > > See if you can help, otherwise i'll run with what i have. (I can't[/color][/color][/color]
                  think[color=blue][color=green][color=darkred]
                  > > > clearly now, the ax just fell here and we lost some very good folk)
                  > > >
                  > > >[/color]
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  • Alvin Bruney

                    #10
                    Re: Thread Exercise for the gurus

                    i got all that but there is an issue with the thread lists. is this list a
                    static list? if it is, then there will be threading corruption and
                    contention as users begin to save data at the same time. i can lock and
                    release but i think that performance will begin to degrade when the app
                    starts to scale up. i'd like to avoid the static approach for that reason.
                    are there any other approaches to having each thread maintain its own list,
                    possibly internal to each thread, that isn't static?

                    "Jon Skeet" <skeet@pobox.co m> wrote in message
                    news:MPG.19ae74 b2b7683a9698a36 0@news.microsof t.com...[color=blue]
                    > Alvin Bruney <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote:[color=green]
                    > > The queries are already written and stacked in an arraylist. I just want[/color][/color]
                    to[color=blue][color=green]
                    > > efficiently pick out blocks of 500 to submit at the same time. for ex.[/color][/color]
                    if i[color=blue][color=green]
                    > > had 2000 records, i would spin 4 threads where each thread's work would[/color][/color]
                    be[color=blue][color=green]
                    > > to insert 500 records. The reasoning here is 500 at the same time among[/color][/color]
                    four[color=blue][color=green]
                    > > threads should be faster than 2000 on the main thread. We've got cpu
                    > > resources to burn.
                    > >
                    > > i'd like to pre-assign - to answer your question. so each thread gets a
                    > > block of 500 and works it until it is done and then waits on the other
                    > > threads to complete.[/color]
                    >
                    > One simple way would be:
                    >
                    > o Work out how many threads you'll need (divide by 500 and limit to 10)
                    > o Create a new list for each thread
                    > o Round-robin copy items from the old list to the new one
                    > o Set the threads going
                    >
                    > The copying shouldn't take long, and as you allocate them in a round-
                    > robin fashion it'll be fair however many threads you have, without
                    > working out any complicated boundary conditions. Once the lists are
                    > written out, you'll need a memory barrier on each of the threads to
                    > make sure that they all get all of the data, but after that there's no
                    > interference between them, so they can each grab items off their own
                    > list without worrying about the other threads.
                    >
                    > --
                    > Jon Skeet - <skeet@pobox.co m>
                    > http://www.pobox.com/~skeet/
                    > If replying to the group, please do not mail me too[/color]


                    Comment

                    • Alvin Bruney

                      #11
                      Re: Thread Exercise for the gurus

                      This is what i have currently
                      int lowerBound = 0, upperBound = 499;


                      if(list.Count < 500)

                      {

                      WriteToDataBase (list);

                      }

                      else

                      {

                      while(upperBoun d < list.Count)

                      {

                      //assign quotas

                      for(int index = lowerBound; lowerBound < upperBound; index++)

                      targetArray.Add (list[index]);

                      //spin a thread to work

                      ThreadStart ThreadDelegate = new ThreadStart(Thr eadWork.ThreadD oWork);

                      Thread saveThread = new Thread(ThreadDe legate);

                      saveThread.Star t();

                      //get ready for more

                      lowerBound = upperBound;

                      upperBound+= 500;

                      targetArray.Cle ar();

                      }

                      }

                      but ThreadDoWork has no access to targetArray. Threadpool would have made it
                      easier since targetArray could be passed in, but it doesn't provide
                      synchronizing mechanisms because all the writes must be done before main
                      thread can move on. For reasons pointed out above this thread, i think a
                      static variable to hold targetArray would be problematic and less efficient.

                      "Chad Myers" <cmyers@N0.SP.A M.austin.rr.com > wrote in message
                      news:eKJiTO6ZDH A.2136@TK2MSFTN GP10.phx.gbl...[color=blue]
                      > Ah, ok. That's good then.
                      >
                      > So just create (#queries)/500 + 1 queues and loop
                      > through the array list and throw the various references
                      > into the various queues and then spawn (#queries)/500 + 1
                      > (or 10, which ever is less) threads and assign the
                      > queues to the threads.
                      >
                      > Each thread can open a connection and execute it's query
                      > and then close the connection (the connections are pooled,
                      > so there's no real performance hit) and then start over
                      > again. I don't know, I suppose you could leave the connection
                      > open, but when you "close" the connection, the connection
                      > sends a reset command to SQL which resets all data, temp tables,
                      > transactions, etc and makes it all nice and clean for the
                      > next statement. It's quite fast, actually.
                      >
                      > What pare are you having the most problems with?
                      >
                      > -c
                      >
                      > "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote
                      > in message news:%23K%233mz 5ZDHA.1280@tk2m sftngp13.phx.gb l...[color=green]
                      > > I stack an array of 2000 queries in an array list. If i don't use[/color]
                      > threads,[color=green]
                      > > it takes about 30 seconds. Boss don't like that, he wants to draw[/color]
                      > blood from[color=green]
                      > > stone. So I figure i can spin 4 threads at 500 queries each at the[/color]
                      > same time[color=green]
                      > > which would give him the performance he is looking for albeit at the[/color]
                      > cost of[color=green]
                      > > cpu i know.
                      > >
                      > > "Chad Myers" <cmyers@N0.SP.A M.austin.rr.com > wrote in message
                      > > news:eQAV$F5ZDH A.1832@TK2MSFTN GP10.phx.gbl...[color=darkred]
                      > > >
                      > > > "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com>[/color][/color]
                      > wrote[color=green][color=darkred]
                      > > > in message news:O%23WNmj2Z DHA.4020@tk2msf tngp13.phx.gbl. ..
                      > > > > I have an array list of queries. The arraylist is variable,[/color][/color]
                      > anywhere[color=green][color=darkred]
                      > > > from 10
                      > > > > to 10000 or more. I'd like to spin threads to take chunks of 500
                      > > > queries out
                      > > > > of that array list, no more than 10 threads (context switching
                      > > > reasons). if
                      > > > > it's less than 500 i spin only one thread.
                      > > > >
                      > > > > I am having trouble building an efficient, clean implementation of
                      > > > this.
                      > > > > Clean means, i don't want to loop from 500 to 1000 if i only have[/color][/color]
                      > 650[color=green][color=darkred]
                      > > > items,
                      > > > > i would like to stop at 650. Efficient means i don't want the code[/color][/color]
                      > to[color=green][color=darkred]
                      > > > be
                      > > > > messy - like what i have now. readable. I don't need the thread[/color][/color]
                      > code[color=green][color=darkred]
                      > > > part, i
                      > > > > need the looping mechanism. I have it done, but it's not clean,[/color][/color]
                      > tight[color=green][color=darkred]
                      > > > or
                      > > > > efficient. I know it can be done better. Efficiency also means
                      > > > efficiently
                      > > > > locking and releasing the datastructure.
                      > > > >
                      > > > > See if you can help, otherwise i'll run with what i have. (I can't
                      > > > think
                      > > > > clearly now, the ax just fell here and we lost some very good[/color][/color]
                      > folk)[color=green][color=darkred]
                      > > >
                      > > >
                      > > > How much performance to you hope to gain here? I mean, the processor
                      > > > can only execute so many instructions per second and splitting[/color][/color]
                      > things[color=green][color=darkred]
                      > > > up into multiple threads is just adding more instructions to be
                      > > > processed.
                      > > >
                      > > > Unless you have a UI that you need to keep active, or you're[/color][/color]
                      > processing[color=green][color=darkred]
                      > > > things that are asynchronous (database, file reads/writes, web[/color][/color]
                      > service[color=green][color=darkred]
                      > > > calls), threading isn't going to help you much.
                      > > >
                      > > > If this is a process of just looping through some calculations and
                      > > > doing them one-by-one, you're probably just better off doing them
                      > > > synchronously.
                      > > >
                      > > > -c
                      > > >
                      > > >[/color]
                      > >
                      > >[/color]
                      >
                      >[/color]


                      Comment

                      • Jon Skeet

                        #12
                        Re: Thread Exercise for the gurus

                        Alvin Bruney <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote:[color=blue]
                        > i got all that but there is an issue with the thread lists. is this list a
                        > static list?[/color]

                        What do you mean by "is this list a static list"? You've got your
                        initial list with all the entries on, and then each thread has its own
                        list. None of them need references in static variables, if that's what
                        you were thinking.
                        [color=blue]
                        > if it is, then there will be threading corruption and
                        > contention as users begin to save data at the same time. i can lock and
                        > release but i think that performance will begin to degrade when the app
                        > starts to scale up. i'd like to avoid the static approach for that reason.
                        > are there any other approaches to having each thread maintain its own list,
                        > possibly internal to each thread, that isn't static?[/color]

                        That's what I'd been suggesting... I can write some sample code if you
                        want.

                        --
                        Jon Skeet - <skeet@pobox.co m>
                        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                        If replying to the group, please do not mail me too

                        Comment

                        • Jon Skeet

                          #13
                          Re: Thread Exercise for the gurus

                          Alvin Bruney <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote:[color=blue]
                          > i got all that but there is an issue with the thread lists. is this list a
                          > static list?[/color]

                          What do you mean by "is this list a static list"? You've got your
                          initial list with all the entries on, and then each thread has its own
                          list. None of them need references in static variables, if that's what
                          you were thinking.
                          [color=blue]
                          > if it is, then there will be threading corruption and
                          > contention as users begin to save data at the same time. i can lock and
                          > release but i think that performance will begin to degrade when the app
                          > starts to scale up. i'd like to avoid the static approach for that reason.
                          > are there any other approaches to having each thread maintain its own list,
                          > possibly internal to each thread, that isn't static?[/color]

                          That's what I'd been suggesting... I can write some sample code if you
                          want.

                          --
                          Jon Skeet - <skeet@pobox.co m>
                          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                          If replying to the group, please do not mail me too

                          Comment

                          • Greg Bacchus

                            #14
                            Re: Thread Exercise for the gurus

                            This is what I'l do:

                            Make a class for the threads, and have a property to which you assign the
                            queries to be processed.

                            class QueryProcessor
                            {
                            private string[] queries;
                            public string[] Queries { set { queries = value; } }

                            public Go()
                            {
                            ...
                            }
                            }

                            or something like that...
                            For each thread create a new instance of that class...

                            QueryProcessor queryProcessor = new QueryProcessor( );
                            queryProcessor. Queries = something;
                            ThreadStart myThreadDelegat e = new ThreadStart(que ryProcessor.Go) ;
                            Thread myThread = new Thread(myThread Delegate);
                            myThread.Start( );

                            that should sort out any corruption.

                            Greg

                            "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
                            message news:%23lIqsoEa DHA.2352@TK2MSF TNGP12.phx.gbl. ..[color=blue]
                            > i got all that but there is an issue with the thread lists. is this list a
                            > static list? if it is, then there will be threading corruption and
                            > contention as users begin to save data at the same time. i can lock and
                            > release but i think that performance will begin to degrade when the app
                            > starts to scale up. i'd like to avoid the static approach for that reason.
                            > are there any other approaches to having each thread maintain its own[/color]
                            list,[color=blue]
                            > possibly internal to each thread, that isn't static?
                            >
                            > "Jon Skeet" <skeet@pobox.co m> wrote in message
                            > news:MPG.19ae74 b2b7683a9698a36 0@news.microsof t.com...[color=green]
                            > > Alvin Bruney <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote:[color=darkred]
                            > > > The queries are already written and stacked in an arraylist. I just[/color][/color][/color]
                            want[color=blue]
                            > to[color=green][color=darkred]
                            > > > efficiently pick out blocks of 500 to submit at the same time. for ex.[/color][/color]
                            > if i[color=green][color=darkred]
                            > > > had 2000 records, i would spin 4 threads where each thread's work[/color][/color][/color]
                            would[color=blue]
                            > be[color=green][color=darkred]
                            > > > to insert 500 records. The reasoning here is 500 at the same time[/color][/color][/color]
                            among[color=blue]
                            > four[color=green][color=darkred]
                            > > > threads should be faster than 2000 on the main thread. We've got cpu
                            > > > resources to burn.
                            > > >
                            > > > i'd like to pre-assign - to answer your question. so each thread gets[/color][/color][/color]
                            a[color=blue][color=green][color=darkred]
                            > > > block of 500 and works it until it is done and then waits on the other
                            > > > threads to complete.[/color]
                            > >
                            > > One simple way would be:
                            > >
                            > > o Work out how many threads you'll need (divide by 500 and limit to 10)
                            > > o Create a new list for each thread
                            > > o Round-robin copy items from the old list to the new one
                            > > o Set the threads going
                            > >
                            > > The copying shouldn't take long, and as you allocate them in a round-
                            > > robin fashion it'll be fair however many threads you have, without
                            > > working out any complicated boundary conditions. Once the lists are
                            > > written out, you'll need a memory barrier on each of the threads to
                            > > make sure that they all get all of the data, but after that there's no
                            > > interference between them, so they can each grab items off their own
                            > > list without worrying about the other threads.
                            > >
                            > > --
                            > > Jon Skeet - <skeet@pobox.co m>
                            > > http://www.pobox.com/~skeet/
                            > > If replying to the group, please do not mail me too[/color]
                            >
                            >[/color]


                            Comment

                            Working...