12 Replies Latest reply on Oct 10, 2008 7:43 PM by SpiderFromMars

    Threads when they are done

    sk8save Level 1
      Let's say I launch a thread without joining it to the current page thread, what happens to it when it's done?

      I'm assuming it stays in memory until the GC comes and cleans it out? How long would that take? Probably a setting in CF?
        • 1. Re: Threads when they are done
          BKBK Adobe Community Professional & MVP
          I'm assuming it stays in memory until the GC comes and cleans it out?
          I think so.

          How long would that take?
          That is up to the garbage collector.

          Probably a setting in CF?
          I don't know of any setting in CF to influence when the GC will operate on a particular object. In fact I suspect such a setting does not exist.

          • 2. Re: Threads when they are done
            sk8save Level 1
            I was testing a few things this Friday and ran out of threads, I couldn't add more. I went into the server monitoring component and noticed that I had a ton of threads running and several others queued. I'm pretty sure it was a collection of all the threads I had created during the afternoon testing my prototype.

            Should I add a terminate call at the end to make sure they get disposed of properly?
            • 3. Re: Threads when they are done
              BKBK Adobe Community Professional & MVP
              What do you mean when you say you couldn't add more? What was Coldfusion's response?

              It is always a good thing to make sure threads come to a graceful end. Also, those that you want to continue should not be high consumers of memory. Otherwise Coldfusion might come to a standstill.

              • 4. Re: Threads when they are done
                sk8save Level 1
                My bad... I did a few more tests today and noticed the queued threads total decreasing, 10 threads a time. At some point, it reached zero, so they do get processed at some point. However, once my queued threads reached 0, I still had 104 threads running and it remained like that until I restarted the service in which it reset to 0.

                I just want to know what to do in order to have threads do what they are supposed to and then stop using resources (memory, cpu, threads, ect...)

                Is calling a terminate at the end of the thread make this possible or simply making the thread run its course?

                My goal is to write a small daemon thread that limits the number of threads running. Once the max thread number drops, then the daemon thread starts another one from the queue. The client is there waiting until his specific thread is done (ajax refresh type thing).

                Since the worker threads will have different timeout points (or there is no possible way to determine how long the thread can take), I can't use event gateways (at least I don't think so at the moment).
                • 5. Re: Threads when they are done
                  BKBK Adobe Community Professional & MVP
                  I did a few more tests today and noticed the queued threads total decreasing, 10 threads a time. At some point, it reached zero, so they do get processed at some point. However, once my queued threads reached 0, I still had 104 threads running and it remained like that until I restarted the service in which it reset to 0.

                  Threads are the proverbial can of worms. Leave the lid open, and they're all over the place.

                  Is calling a terminate at the end of the thread make this possible or simply making the thread run its course?

                  The terminate action of the cfthread tag instructs Coldfusion to stop processing the thread.

                  My goal is to write a small daemon thread that limits the number of threads running.

                  I get the feeling that might be risky. I would leave that kind of control over to Coldfusion. See what you can do with the settings on the page Server Settings => Request Tuning in the Administrator.

                  • 6. Re: Threads when they are done
                    sk8save Level 1
                    What I should have said was that I want to limit the amount of very specific threads that my own application launches - I'm on a corporate shared environment.
                    • 7. Threads when they are done
                      BKBK Adobe Community Professional & MVP
                      I don't know how to place a global limit on threads without involving the Coldfusion Administrator. I wonder whether it's even possible. It involves some complexity. All requests involve threads, and there are thread groups, for example, web and jrun. In my opinion all you can, and should, do is make sure that your application's threads come to a graceful and timely end, and that they consume minimal memory.

                      • 8. Re: Threads when they are done
                        sk8save Level 1
                        Not sure if this clears things up but all of this is related to the tag cfthreads, those are the ones I want to limit/control in my app.
                        • 9. Re: Threads when they are done
                          BKBK Adobe Community Professional & MVP
                          The only facility I know of in cfthread is to call the terminate action on a named thread.

                          • 10. Re: Threads when they are done
                            SpiderFromMars Level 1
                            When I terminate a thread in this manner to kill runaway thread:

                            <cfloop list="#t#" index="i">
                            <cfif findNoCase('RUNNING', evaluate('#i#.status')) gt 0>
                            <cfsilent> <cfthread action="terminate" name="#i#" /> </cfsilent>
                            </cfif>
                            </cfloop>

                            Each thread that is terminated gets logged in the application log of the CF administrator.

                            How do I keep this from getting logged?
                            • 11. Re: Threads when they are done
                              Level 7
                              > How do I keep this from getting logged?

                              Write your code so the threads finish gracefully, without you having to
                              kill them?

                              -- or --

                              If you have to kill these runaway threads... why is it a problem that their
                              death gets logged?

                              --
                              Adam
                              • 12. Re: Threads when they are done
                                SpiderFromMars Level 1
                                >> How do I keep this from getting logged?

                                >Write your code so the threads finish gracefully, without you having to
                                >kill them?


                                The thread is very simple. Only a few lines of code and it finishes just fine. But, because of the nature of application, about a hundred threads are spawned and the garabage collector normally kills all of them by the time the join is completed. However, sometimes some of those threads hang around for no apparent reason. So, after the threading has been completed, then I would like to check to make sure they have been freed up so they are available for other requests.


                                -- or --

                                >If you have to kill these runaway threads... why is it a problem that their
                                >death gets logged?


                                Because killing a thread should not be considered an application error.