复制代码 代码如下:
Thread t = new Thread( new ThreadStart( st.WorkerMethod ) );
t.Start ();
public void WorkerMethod()
{
Console.WriteLine ("AA ");
}
复制代码 代码如下:
public class SimpleThread
{
private string procParameter = "";
public SimpleThread (string strPara)
{
procParameter = strPara;
}
public void WorkerMethod()
{
Console.WriteLine ("参数输入为: " + procParameter);
}
}
class MainClass
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
SimpleThread st = new SimpleThread("这是参数字符串!");
Thread t = new Thread( new ThreadStart( st.WorkerMethod ) );
t.Start ();
t.Join (Timeout.Infinite);
}
}
复制代码 代码如下:
for (int t = 0; t < x; t++)//X为线程数
{
Thread thread = new Thread(new ThreadStart(Sort));//Sort排序的方法
thread.Name = Convert.ToString(t);
thread.Start();
}
public void Sort()
{
try
{
while (true)
{
swaped = false;
for (int j = 0; j < valueArray.Length - 1; j++)
{
lock (typeof(Thread))//锁定
{
if (valueArray[j] > valueArray[j + 1])
{
int T = valueArray[j];
valueArray[j] = valueArray[j + 1];
valueArray[j + 1] = T;
swaped = true;
}
}
}
Thread.Sleep(1);
if (!swaped) { break; }
}
Thread.CurrentThread.Abort();
}
catch (Exception ex)
{
if (Interlocked.Increment(ref threadCounter) == Convert.ToInt64(ddlThreadNum.SelectedItem.ToString().ToString().Trim()))
Display();
}
}
新闻热点
疑难解答
图片精选