Multi-thread mail analysis-Maximum number of threads in a process

Source: Internet
Author: User
This is the first article posted on the blog site. I have read many of the first articles about the virgins on the TV station.

I have been studying the issue of multi-threaded mail over the past few days. I read some articles about multi-thread mutex and synchronization, and the final problem is stuck. How can I ensure that the same email address will not be extracted from the database under multi-thread conditions. if the Administrator feels that the technical content is too low, delete it :)

The following solutions are available on the Internet:

1. Remove one entry from the database and lock it with lock or mutex. I think this must be a crazy idea. 1 million frequent database operations, I miss that no one will want to do this. I thought of a work und: Put the data in the cache and delete it in a message queue or an ordered array.

2. Divide the total number of logs by the number of threads to get several intervals in which the threads are sequentially retrieved. For example, the content I extracted from the database is as follows:
ID name email
1 name1 name1@163.com
2 name2 name2@163.com
3 name3 name3@163.com
.........
100 name100 name100@163.com
101 name101 name101@163.com
102 name102 name102@163.com
.........
200 name200 name200@163.com
..................
300 name300 name300@163.com
..................
400 name400 name400@163.com
Then there can be divided into four threads, respectively taking data from 1-100,101-200,201-300,301-400 to send emails, but a small bug, that is, the ID must be numeric and ordered. However, I can consider a work und and add an auto-incrementing column.

3. this method is from the discuznt version. This forum is the earliest open-source commercial forum in China. I am studying it. net. the Code is as follows: 1 datatable dt = databaseprovider. getinstance (). maillisttable (usernamelist. text );
2 thread [] lthreads = new thread [DT. Rows. Count];
3 int COUNT = 0;
4
5 foreach (datarow DR in DT. Rows)
6 {
7 emailmultithread EMT = new emailmultithread (Dr ["username"]. tostring (), Dr ["email"]. tostring (), subject. Text, body. Text );
8 lthreads [count] = new thread (New threadstart (EMT. Send ));
9 lthreads [count]. Start ();
10
11 if (count> = percount)
12 {
13 thread. Sleep (5000 );
14 COUNT = 0;
15}
16 count ++;
17
18 // threadpool. queueuserworkitem (New waitcallback (Sendmail), String. Format ("http://bbs.ent.tom.com/forum/view_thread.php? Forumid = 1 & threadid = {0} & backurl = http % 3A % 2f % 2fbbs.ent.tom.com % 2 Fforum % 2flist_thread.php % 3 fforumid % 3d1% 26 Page % 3d1% 26 sort % 3d0", pagenumber. tostring ()));
19}

It is hard to imagine that it uses this method. How many threads are created for each piece of data, which returns to my question: how many threads can a process have at most?

The following question is: which solution is better? Or is there a better solution?
In my mind, the second one may be better, with lower system resource occupation and higher efficiency.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.