using System;
using System.Threading;
public class CreateThreading
...{ //線程的暫停與恢復Suspend吊,懸掛,Resume;Sleep(int),實例.Interrupt終止線程
//Monitor.Wait(this)暫停當前線程,Monitor.Pulse(this)使最先被暫停的線程繼續
//實例.Join將實例線程插入到當前語句.ThreadPool線程池
Thread ThreadA, ThreadB; int addSum;
int intArg;
static void Main(string[] args)
...{//啟動時加上運行參數後,主線程等待B線程運行結束才繼續向下執行
CreateThreading myCThr = new CreateThreading();
myCThr.intArg = args.Length;
for (int i = 0; i < myCThr.intArg; i++) Console.WriteLine(args[i]);
myCThr.Start();
}
public void Start()
...{
ThreadStart myThreadStart1 = new ThreadStart(PrintOddNumber);
ThreadA = new Thread(myThreadStart1);
ThreadA.Name = "A ";
//ThreadStart myThreadStart2 = new ThreadStart(PrintNumber);
; ThreadB = new Thread(new ThreadStart(PrintNumber));
ThreadB.Name = "B ";
ThreadA.Start();
ThreadB.Start();
if (intArg > 0) ThreadB.Join();//此句表示當前線程被暫停,線程B結束後,後面的語句才被執行
//相當於電路原來並聯的拿出來串聯的此處
CreateThreading mycls = new CreateThreading();
Thread[] Theads=new Thread[3];
for (int i = 0; i < 3; i++)
...{
Theads[i] = new Thread(new ThreadStart(mycls.DOSum));
Theads[i].Name =i.ToString();
Theads[i].Start();
}
Thread.Sleep(100);//主線程暫停等待
Console.WriteLine("---------------主線程結束,其他線程仍要執行完畢-------------------");
}
private void PrintOddNumber()
...{
for (int i = 0; i < 30; i++)
...{
if (i % 2 != 0)
...{
Console.WriteLine("線程{0}輸出奇數{1}", Thread.CurrentThread.Name, i);
Thread.Sleep(20);
}
}
}
private void PrintNumber()
...{
for (int i = 0; i < 20; i++)
...{
if (i % 2 == 0)
...{
Console.WriteLine(" 線程{0}輸出偶數{1}", Thread.CurrentThread.Name, i);
Thread.Sleep(20);
}
if (i > 10) ThreadA.Join(100);// 當運行到100時當前線程被停止直到線程1運行完畢才返回繼續
}
}
void DOSum()
...{
for (int i = 0; i < 10; i++)
...{
lock (this)
...{
addSum += 2; Thread.Sleep(10);
int m = int.Parse(Thread.CurrentThread.Name);
string st = new string('' '', m);
Console.WriteLine(st + " 線程" + Thread.CurrentThread.Name + " 執行第" + i + "次,AddSum=" + addSum);
}
}
}
}
using System;
using System.Threading;
public class clsThreadPool
...{ //線程的暫停與恢復Suspend吊,懸掛,Resume;Sleep(int),實例.Interrupt終止線程
//Monitor.Wait(this)暫停當前線程,Monitor.Pulse(this)使最先被暫停的線程繼續
//實例.Join將實例線程插入到當前語句.ThreadPool線程池
static void Main(string[] args)
...{
clsThreadPool myThrP = new clsThreadPool();
int putoff = 8000;//------主線程延時結束的時間ms------------
Console.WriteLine("-----------------------");
for (int i = 0; i < 4; i++)
...{ //針對某方法的線程池
WaitCallback myWaitCBK = new WaitCallback(myThrP.WorkItem);//線程執行入口
ThreadPool.QueueUserWorkItem(myWaitCBK, i + 1);//放入線程池,線程池提供一個線程開始運行
Thread.Sleep(3);
}
Console.WriteLine("------------------線程池中其他線程都已經啟動------------------!");
Thread.Sleep(putoff);//主線程的時間,時間和線程池中的線程並列,/如果延時較少主線程結束
//線程池中的其他線程將被中斷,因為線程池被釋放其中的線程被自動結束所以不同於一般的多線程
Console.WriteLine("------------主線程結束,線程池中其他線程也隨即被中斷-----------!");
}
public void WorkItem(object j)
...{
int itemInt= Thread.CurrentThread.GetHashCode();
lock (this)
...{
string str = new string('' '', itemInt);
for (int i = 0; i <5+ int.Parse(j.ToString())*2; i++)
...{
Console.WriteLine(str+"使用的線程 ID="+itemInt + " " + i + " ");
Thread.Sleep(80);//當延時較大時,下一個線程開始就要重新分配一個線程,
//否則,下一個線程將使用上一個線程
}
Console.WriteLine(str + itemInt + "此線程工作結束!");
}
}
}
using System;
/**//// <summary>
/// Summary description for CastToInterface
/// </summary>
public class CastToInterface//接口的練習
...{
static void Main(string[] args)
...{
Square mysquare = new Square();
if (mysquare is IMeasure && mysquare is IWhight)//防止發生接口轉型的錯誤
...{
IMeasure myIMeasure = (IMeasure)mysquare;//= mysquare as IMeasure;
IWhight myIWeight = (IWhight)mysquare;//= mysquare as IMeasure;
double sLength, density;//polygon[paoligen]多邊形
Console.Write("Input margin length:"); sLength=double.Parse(Console.ReadLine());
Console.Write("Input matter density:"); density = double.Parse(Console.ReadLine());
myIMeasure.Length(sLength);
myIMeasure.Area(sLength);
myIWeight.GetWeight(density, sLength);
}
Console.ReadLine();
}
}
//下面定義一個純抽象(Abstract)類型接口
interface IMeasure
...{
void Length(double l);
void Area(double l);
double GetVolume(double l);
}
interface IWhight//體積接口
...{
double GetWeight(double density, double l);
}
class Square : IMeasure,IWhight//繼承接口的類
...{
public double GetVolume(double l)
...{
double volume;
volume = Math.Pow(l, 3);
Console.WriteLine
("邊長為{0}的正方形體積={1}", l, volume);
return volume;
}
public double GetWeight(double density, double l)
...{
double squareVolume = GetVolume(l);
double Weight = density * squareVolume;
Console.WriteLine
("邊長為{0}密度為{1}的正方形重量={2}", l,density, Weight);
return Weight;
}
public void Length(double l)
...{
double squareLength;
squareLength = 4 * l;
Console.WriteLine
("邊長為{0}的正方形周長={1}",l,squareLength);
}
public void Area(double l)
...{
double area;
area = Math.Pow(l, 2);
Console.WriteLine
("邊長為{0}的正方形面積={1}", l, area);
}
//public void Volume(double l)
//{
// double volume;
// volume = Math.Pow(l, 3);
// Console.WriteLine
// ("邊長為{0}的正方形體積={1}", l, volume);
//}
}