C#多线程共享数据
在多線程編程中,我們經(jīng)常要使用數(shù)據(jù)共享.C#中是如何實現(xiàn)的呢?很簡單,只要把你要共享的數(shù)據(jù)設(shè)置成靜態(tài)的就可以了.關(guān)鍵字static .如下:
static Queue q1=new Queue();
static int b=0;
在這里我定義了一個整形變量b和隊列q1.
接下去就可以創(chuàng)建多線程代碼了.如下:
MyThread myc;
Thread[] myt;
myt=new Thread[10];
myc=new MyThread();
for(int i=0;i<10;++i)
{
myt[i]=new Thread(new ThreadStart(myc.DoFun));
// System.Console.WriteLine("<<{0}>>",myt[i].GetHashCode());
myt[i].Start();
Thread.Sleep(1000);
}
你可能驚奇的發(fā)現(xiàn)這里使用了一個類實例myc.在起初的設(shè)計中我使用了MyThread數(shù)組,對于本例來說這沒有什么關(guān)系,當(dāng)線程要使用不同的操作時,那就要使用其他的類實例了.
以下是完整的代碼:
using System;
using System.Threading;
using System.Collections;
namespace shareThread
{
class MyThread
{
static Queue q1=new Queue();
static int b=0;
public void DoFun()
{
lock(this)
{
++b;
q1.Enqueue(b);
}
System.Console.WriteLine("B:{0}--------------",b);
PrintValues( q1 );
}
public static void PrintValues( IEnumerable myCollection )
{
System.Collections.IEnumerator myEnumerator = myCollection.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.Write( "/t{0}", myEnumerator.Current );
Console.WriteLine();
}
}
/// <summary>
/// Class1 的摘要說明。
/// </summary>
class ClassMain
{
/// <summary>
/// 應(yīng)用程序的主入口點。
/// </summary>
[STAThread]
static void Main(string[] args)
{
MyThread myc;
Thread[] myt;
myt=new Thread[10];
myc=new MyThread();
for(int i=0;i<10;++i)
{
myt[i]=new Thread(new ThreadStart(myc.DoFun));
// System.Console.WriteLine("<<{0}>>",myt[i].GetHashCode());
myt[i].Start(); //線程運行
Thread.Sleep(1000);//主線程睡眠
}
System.Console.Read();//等待完成,dos窗口不會馬上關(guān)閉了.
}
}
}?
?
static Queue q1=new Queue();
static int b=0;
在這里我定義了一個整形變量b和隊列q1.
接下去就可以創(chuàng)建多線程代碼了.如下:
MyThread myc;
Thread[] myt;
myt=new Thread[10];
myc=new MyThread();
for(int i=0;i<10;++i)
{
myt[i]=new Thread(new ThreadStart(myc.DoFun));
// System.Console.WriteLine("<<{0}>>",myt[i].GetHashCode());
myt[i].Start();
Thread.Sleep(1000);
}
你可能驚奇的發(fā)現(xiàn)這里使用了一個類實例myc.在起初的設(shè)計中我使用了MyThread數(shù)組,對于本例來說這沒有什么關(guān)系,當(dāng)線程要使用不同的操作時,那就要使用其他的類實例了.
以下是完整的代碼:
using System;
using System.Threading;
using System.Collections;
namespace shareThread
{
class MyThread
{
static Queue q1=new Queue();
static int b=0;
public void DoFun()
{
lock(this)
{
++b;
q1.Enqueue(b);
}
System.Console.WriteLine("B:{0}--------------",b);
PrintValues( q1 );
}
public static void PrintValues( IEnumerable myCollection )
{
System.Collections.IEnumerator myEnumerator = myCollection.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.Write( "/t{0}", myEnumerator.Current );
Console.WriteLine();
}
}
/// <summary>
/// Class1 的摘要說明。
/// </summary>
class ClassMain
{
/// <summary>
/// 應(yīng)用程序的主入口點。
/// </summary>
[STAThread]
static void Main(string[] args)
{
MyThread myc;
Thread[] myt;
myt=new Thread[10];
myc=new MyThread();
for(int i=0;i<10;++i)
{
myt[i]=new Thread(new ThreadStart(myc.DoFun));
// System.Console.WriteLine("<<{0}>>",myt[i].GetHashCode());
myt[i].Start(); //線程運行
Thread.Sleep(1000);//主線程睡眠
}
System.Console.Read();//等待完成,dos窗口不會馬上關(guān)閉了.
}
}
}?
?
總結(jié)
- 上一篇: C#控制远程计算机的服务
- 下一篇: 已知2个整形数据a,b.不使用if,?: