0

Is it possible, and is there a code example. On how to make multiple instances of a single thread.

a quick example is just

Thread foo = new Thread(testThread); foo.Start(); // but start more than once instance 

@Andrew Well, when using a web client to download a page it takes about 0.5 seconds. When I make multiple threads that do the same task (copy and pasting the same thread under different function names and running them simultaneously using a global list and indexer for the for loop. With 4 copy threads it gets a page downloaded at about 0.15 seconds.

3 times faster from 4 threads is nice but I want a more clean solution

10
  • 5
    If you are creating lots, either ThreadPool or Task would be more suitable than Thread Commented May 29, 2011 at 19:35
  • 2
    @John: If you disagree with someone, then disagree with them. Just be civil about it. Commented May 29, 2011 at 20:13
  • 1
    Also, and this is not-moderator-me talking, it's not entirely clear what you're asking. If you start the same thread multiple times, that is, you create multiple threads all executing the same thread method, then you will just do that job multiple times, it doesn't make the job run once, faster. As such, your statement that you download a page in 0.5 seconds on one thread, and 0.15 seconds when using 4 threads is a bit strange, since you would in fact download the file 4 times. Could it be the server/proxy caches it for you? Please clarify. Commented May 29, 2011 at 20:23
  • 1
    Or is perhaps "download a page" short for downloading a web page and all related files, like images, stylesheets, javascript files? If so, are you asking how to structure the code to have one common list of "things to download", and have X threads running to download those things? If so then I think the answer is just to use the thread pool, as @Marc has already stated. But as I said, I'm probably not understanding all of your question so I might get something wrong. Commented May 29, 2011 at 20:25
  • 1
    Pretty much, I need multiple threads to go through a list of strings using a webclient. With a webclient I use the function DownloadString() to get the source code to the page. While that function is downloading in one thread, another thread will get the next url in queue and download that using the same function/thread. This way instead of waiting for 1 page to download at a time. I download as fast as possible. Commented May 29, 2011 at 20:31

1 Answer 1

3

The thread instance is not the same as the thread logic. So you can create multiple threads using the same method, you don't have to copy-and-paste the code into separate identical ones:

for( int i=0; i<4; i++ ) (new Thread(threadProc)).Start(); 

This is legal and may do what you need -- but per the comments, if you do need parallelism then it would be wiser to use the ThreadPool or else use TPL Tasks:

for( int i=0; i<4; i++ ) ThreadPool.QueueUserWorkItem(threadProc); for( int i=0; i<4; i++ ) (new Task( threadProc )).Start(); 

These variations can save you the overhead of creating and destroying individual threads and can achieve better overall utilization.

All these approaches have variations where you can pass parameters to your function, for example the variable i or other information to divide the work. See the docs for details, e.g. http://msdn.microsoft.com/en-us/library/kbf0f1ct.aspx

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.