basic threading question

G

Guest

Hi,

In my application I create 4 new threads that each do the same code, but do
different amounts of work. The process repeats over and over, so I create
the 4 threads many times. I think however I am not killing of the threads
when I am done, so I am ending up with 8,12,16 etc threads as it repeats.

When each thread completes its work load, how do I kill it off properly.

For example

Thread1 = New Thread(AddressOf ThreadWorkLoad1Job.runBatch)
Thread1.Start()

Thread2 = New Thread(AddressOf ThreadWorkLoad2Job.runBatch)
Thread2.Start()

sub runbatch
process a bunch of data in an sql table
end sub


When the runbatch code completes it seems that the thread(1 or 2) remains
running even though it does no work.

I need something like
sub runbatch
process a bunch of data in an sql table
thread.kill.
end sub
 
D

DickGrier

I presume that you think you want to use call something like Thread1.Abort()
to kill the associated thread? You can do this, though I think there are
situations where the thread may not terminate. What I generally do is to
create a thread that has a process loop. In other words,
ThreadWorkLoad2Job.runBatch is a method that exits automatically when the
loop exits. The thread then dies by itself. This allows you to force the
loop to exit by checking some sort of flag that you set from the program --
but normally, whenever the process inside the loop is finished, it simply
exits, and the thread ends. Of course, you may not even need a loop (but a
loop allows you to check a flag). If you do use a flag, it is good practice
to close any connections that may currently be open, etc.

IMO, a good design pattern is to have threads that do something interesting
and then end on their own. Of course, it is up to the Garbage Collector to
return any resources used, and this may not be immediate.

If you do use Abort(), things should be cleaned up for you, but this sort of
thing might be the source of trouble, and Abort() causes an exception, which
adds some load to the system.

AFAIK, when the runBatch method that you showed in your pseudo code exits --
the thread also exits, so it doesn't have to be killed (Abort()). If you
are seeing a thread count increasing over time, I suspect that the Garbage
Collector simply hasn't recovered those resources yet (but it should,
eventually).

System.GC has methods that allow you to interact with the Garbage Collector,
for example the Collect method. However, if you are not seeing any
performance issues associated with these thread, then (again, IMO) it may be
best to simply leave well enough alone.

Dick

--
Richard Grier, Consultant, Hard & Software 12962 West Louisiana Avenue
Lakewood, CO 80228 303-986-2179 (voice) Homepage: www.hardandsoftware.net
Author of Visual Basic Programmer's Guide to Serial Communications, 4th
Edition ISBN 1-890422-28-2 (391 pages) published July 2004, Revised July
2006.
 
P

Phill W.

NoSpammer said:
In my application I create 4 new threads that each do the same code, but
do different amounts of work. The process repeats over and over, so I
create the 4 threads many times. I think however I am not killing of the
threads when I am done, so I am ending up with 8,12,16 etc threads as
it repeats.

AFAIK a Thread is just a /method/ that happens to be run independently
on the program's main flow of execution. When that method ends, the
associated Thread should die off with it.
When each thread completes its work load, how do I kill it off properly.

You shouldn't need to.
When the runbatch code completes it seems that the thread (1 or 2)
remains running even though it does no work.

Are you /sure/ the method has finished?
Are there any loop conditions in there that you might be "stuck" in?
Are there any controls being used that might be keeping the thread alive?
are all your database connections being properly Close'd/Dispose'd?

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top