using System;
using System.Threading;
namespace C17_7
{
public class MyThread
{
//定義一個資料欄位
public int m_x = 0;
//定義一個寫資料的方法
public void WriteThread() {
Thread.Sleep(1000);
m_x = 5;
}
//定義一個讀數據的方法
public void ReadThread10() {
int a = 10;
for (int y = 0; y < 3; y++)
{
string s = "ReadThread10:";
s += Convert.ToString(a) + "#";
s += a * m_x;
Console.WriteLine(s);
Thread.Sleep(1000);
}
}
//定義一個讀數據的方法
public void ReadThread20() {
int a = 20;
for (int y = 0; y < 3; y++)
{
string s = "ReadThread20:";
s += Convert.ToString(a) + "#";
s += a * m_x;
Console.WriteLine(s);
Thread.Sleep(1000);
}
}
}
public class Test
{
static void Main()
{
//分別建立3個執行緒來運行上面的3個方法,並使用沒有同步的機制的方法來控制執行的運行
MyThread myThread = new MyThread();
new Thread(new ThreadStart(myThread.WriteThread)).Start();
new Thread(new ThreadStart(myThread.ReadThread10)).Start();
new Thread(new ThreadStart(myThread.ReadThread20)).Start();
}
}
}
沒有留言:
張貼留言