public void action() {
switch (num)
{
case 0:
Control();
break;
case 1:
readText();
break;
case 2:
Control();
readText();
break;
default:
break;
}
}
/// <summary>
/// 开始抓取
/// </summary>
private void Control()
{
for (int i = statrNum; i < Endnum; i += 25)
{
string webUrl = string.Format(pi.WebSites, i);
listUrl.Add(webUrl);
} int j = 0;
while (true)
{
if (_threadhash.Count >= threadNO)
{
_threadhash.Clear();
Thread.Sleep(1000);
continue;
}
string urlNo = "";
string str = listUrl[j];
for (int i = 0; i < str.Length; i++)
{
if (Char.IsNumber(str, i) == true)
{
urlNo += str.Substring(i, 1);
}
else
{
if (str.Substring(i, 1) == ",")
{
urlNo += str.Substring(i, 1);
}
}
}
NewThread thread1 = new NewThread(listUrl[j], int.Parse(urlNo) + 25);
Thread s = new Thread(thread1.OutputN);
s.Start();
_threadhash[j] = s;
j++; if (j >= listUrl.Count)
{
break;
}
Thread.Sleep(1000);
}
} #region---------------------操作数据库readText()---------------
//读取文本,操作数据库
public void readText()
{
Console.WriteLine("yes000000000000000000000000000000000000000000000");
}
static void Main(string[] args)
{
Program pr = new Program();
pr.action();
}结果是: 线程1
线程2
yes000000000000000000000000000000000000000000000
线程3
线程4
....
...多线程thread
switch (num)
{
case 0:
Control();
break;
case 1:
readText();
break;
case 2:
Control();
readText();
break;
default:
break;
}
}
/// <summary>
/// 开始抓取
/// </summary>
private void Control()
{
for (int i = statrNum; i < Endnum; i += 25)
{
string webUrl = string.Format(pi.WebSites, i);
listUrl.Add(webUrl);
} int j = 0;
while (true)
{
if (_threadhash.Count >= threadNO)
{
_threadhash.Clear();
Thread.Sleep(1000);
continue;
}
string urlNo = "";
string str = listUrl[j];
for (int i = 0; i < str.Length; i++)
{
if (Char.IsNumber(str, i) == true)
{
urlNo += str.Substring(i, 1);
}
else
{
if (str.Substring(i, 1) == ",")
{
urlNo += str.Substring(i, 1);
}
}
}
NewThread thread1 = new NewThread(listUrl[j], int.Parse(urlNo) + 25);
Thread s = new Thread(thread1.OutputN);
s.Start();
_threadhash[j] = s;
j++; if (j >= listUrl.Count)
{
break;
}
Thread.Sleep(1000);
}
} #region---------------------操作数据库readText()---------------
//读取文本,操作数据库
public void readText()
{
Console.WriteLine("yes000000000000000000000000000000000000000000000");
}
static void Main(string[] args)
{
Program pr = new Program();
pr.action();
}结果是: 线程1
线程2
yes000000000000000000000000000000000000000000000
线程3
线程4
....
...多线程thread
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Threading;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;
using MySql.Data.MySqlClient;
using System.Collections;namespace CRReptile
{
class Program
{
ParametInfo pi = new ParametInfo();
private static string mySqlString = ConfigurationManager.ConnectionStrings["mysql"].ConnectionString;
private static List<string> listUrl = new List<string>();
private int threadNO = 10;
private int num = 2;
private int Endnum = 25;
private int statrNum = 0;
private static ManualResetEvent ent = new ManualResetEvent(false);
public static Hashtable _threadhash = new Hashtable();
public Program() {
threadNO = pi.ThreadNo;
num = pi.Operating;
Endnum = pi.LineNum;
statrNum = pi.StatsNum;
} public void action() {
switch (num)
{
case 0:
Control();
break;
case 1:
readText();
break;
case 2:
Control();
Console.read();
readText();
break;
default:
break;
}
}
/// <summary>
/// 开始抓取
/// </summary>
private void Control()
{
for (int i = statrNum; i < Endnum; i += 25)
{
string webUrl = string.Format(pi.WebSites, i);
listUrl.Add(webUrl);
} int j = 0;
while (true)
{
if (_threadhash.Count >= threadNO)
{
_threadhash.Clear();
Thread.Sleep(1000);
continue;
}
string urlNo = "";
string str = listUrl[j];
for (int i = 0; i < str.Length; i++)
{
if (Char.IsNumber(str, i) == true)
{
urlNo += str.Substring(i, 1);
}
else
{
if (str.Substring(i, 1) == ",")
{
urlNo += str.Substring(i, 1);
}
}
}
NewThread thread1 = new NewThread(listUrl[j], int.Parse(urlNo) + 25);
Thread s = new Thread(thread1.OutputN);
s.Start();
_threadhash[j] = s;
j++; if (j >= listUrl.Count)
{
break;
}
Thread.Sleep(1000);
}
} #region---------------------操作数据库readText()---------------
//读取文本,操作数据库
public static void readText()
{
Console.WriteLine("yes000000000000000000000000000000000000000000000");
//string file = string.Format("..\\Debug\\templog\\templog{0}.txt");
//try
//{
// List<string> sqlStr = new List<string>();
// using (StreamReader read = new StreamReader(file))
// {
// string line;
// while ((line = read.ReadLine()) != null)
// {
// Console.WriteLine(line);
// sqlStr.Add(line);
// }
// }
// OperatingData(sqlStr);
//}
//catch (Exception)
//{
// Console.WriteLine("当前文件不存在");
// Console.ReadKey();
//}
}
//对数据库进行操作
public void OperatingData(List<string> sqlStr)
{ using (MySqlConnection con = new MySqlConnection(mySqlString))
{
con.Open();
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = con;
MySqlTransaction tran = con.BeginTransaction();
cmd.Transaction = tran;
try
{
for (int n = 0; n < sqlStr.Count; n++)
{
string strsql = sqlStr[n].ToString();
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
}
//后来加上的
if (n > 0 && (n % 500 == 0 || n == sqlStr.Count - 1))
{
tran.Commit();
tran = con.BeginTransaction();
}
} }
catch (MySqlException E)
{
tran.Rollback();
Console.WriteLine("当前数据库表不存在");
}
}
}
#endregion static void Main(string[] args)
{
Program pr = new Program();
pr.action();
}
}
}
下面就是看你代码执行线程了,假定你的主线程ID为1,那么action方法就是在线程1中执行的,进入内部分支判断后进入到num=2的分支,然后还是线程1中执行Control();,但是在Control方法内部又新开了几个线程,ID分别为2、3、4、5……,总之不可能在线程1上执行了,然后等那几个线程开始后,Control执行结束,线程1执行下面的readText方法,这不是很明显的多线程并行执行吗?谁都不会妨碍谁。
public void action()
{
switch (num)
{
case 2:
using (control()) readText();
break;
}
}
private fastCSharp.threading.task control()
{
fastCSharp.threading.task task = new fastCSharp.threading.task(threadNO);
foreach (string url in listUrl)
{
NewThread thread1 = new NewThread(listUrl[j], int.Parse(urlNo) + 25);
task.Add(thread1.OutputN);
}
return task;
}