C# 多线程练习
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Collections; using System.IO; namespace ThreadTest { class Program { public static Thread[] iThread = new Thread[5]; public static ThreadStart[] iThreadStart = new ThreadStart[5]; public static string str = ""; static void Main(string[] args) { for (int i = 0; i < 5; i++) { CreateThread(i); } WriterLog(); } //Create一个线程 public static void CreateThread(int i) { iThread[i] = new Thread(new ParameterizedThreadStart(proc)); iThread[i].Start(i); str += "线程<" + i.ToString() + ">启动!!!\r"; Console.WriteLine("线程<" + i.ToString() + ">启动!!!"); } // public static void proc(object i) { int m = (int)i; for (int n = 1; n <= 10; n++) { str += "线程<" + m.ToString() + ">运行" + n.ToString() + "次\r"; Console.WriteLine("线程<" + m.ToString() + ">运行" + n.ToString() + "次"); } } //写日志文件 public static void WriterLog() { string path = @"E:\Work\log.txt"; StreamWriter sw = new StreamWriter(path); sw.WriteLine(str); sw.Close(); } } }
种一棵树最好的时间是十年前,其次是现在.