Click here to Skip to main content
15,896,730 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
My requirements are:
1. Query data from url1 and url2
2. Compare two urls' data find valuable data.
3. Make 3 times deal.


I used 20 accounts to query data. And one account made deal. I create 40 long term threads for querying data(20 url1, 20 url2). And use 3 task to make deal when get some valuable data. Every account query data interval is 500 ms. I want every 20 ms to query data. How to make the thread on right schedule? And how many threads will be reasonable?


Environment: windows server 2003 sp2
.net 4.0
Posted
Comments
Sergey Alexandrovich Kryukov 30-Apr-14 14:36pm    
I would need more knowledge on your application to get a better advice, but 1) your approach can work; 2) it is, most likely, far from optimal; it's better to reduce the number of threads to minimum, find the ways to reuse the same thread. You won't be able to do much really in parallel, so create threads per logical units of independent functionality; forget about performance or throughput.
—SA
scut_terry 1-May-14 5:33am    
My app need high performance. <br>
Every thread is sleep or block in most of time. I don't know how to make these thread requset in every 20 ms for the getting data are lastest.<br>
My approach is:<br>
1. call HttpWebRequest getResponse() method to get data(block the thread), the return data will set a member var.<br>
2. the 2 urls data are ready and compare them. (the compare method is synchronized). <br>
3. create 3 tasks to make deals when get some valuable data. if there is not valuable data. do nothing in this step.<br>
4. finally the thread sleep about 500 ms to wait next request.(request too often in same account will not return data)
Sergey Alexandrovich Kryukov 1-May-14 8:33am    
The idea to have many threads is wait state is no so bad. In such state, a thread waste zero CPU time until it is waken up, which ultimately happens on hardware interrupt (timer or, say, network). But your idea to predefine time slices is no good... Your threads should be blocked by read/write calls themselves. No data — a thread is at wait state. Just think about it.
—SA
scut_terry 1-May-14 10:23am    
thank you. i'll try it.
Sergey Alexandrovich Kryukov 1-May-14 13:06pm    
Great. And then, if you have some more particular concerns or more particular problems, please ask another, more particular question.
If you do, please comment on the present comment for me to get some notification; this is an interesting topic.
—SA

1 solution

Generally you should only create threads equal to your core count, if you are on .net 4 then you should use Task instead of Thread which are lighter and faster.
The Basics of Task Parallelism via C#[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 30-Apr-14 14:34pm    
Of course not. Some problems require threads by logical reasons, and they will work even on a single-core single-CPU system.
Besides, there are cases when you really need threads, not Tasks (which are based on threads). You are thinking in terms of improving throughput by parallel execution, but this is not the only, and not even the primary purpose of threads.

Sorry, but your answer is utterly misleading.

—SA
[no name] 1-May-14 22:01pm    
You have made a definitive statement without any reason or analysis. The answer is actually too simplistic to debate.
Mehdi Gholam 2-May-14 0:47am    
See the following great resource : http://mechanical-sympathy.blogspot.com/
[no name] 2-May-14 1:25am    
Please assist me by explaining how this link applies to my comment, your answer or the question.
Mehdi Gholam 2-May-14 1:38am    
Threads are resource intensive and to avoid context switching (OS + cpu) and increasing latency you are limited to the number of cores you have (effectively the work you can do in parallel).

In the case of the OP, he asked a generic question and latency was a concern.

In the general case the use of light weight "threads" like Tasks are better which came to the forefront with Erlang (I routinely create 30k+ tasks at a time when indexing in RaptorDB).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900