執行緒委派方式
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
//http://www.dotblogs.com.tw/atowngit/archive/2009/12/19/12547.aspx
namespace Thread_Practice
{
class Program
{
static void Main(string[] args)
{
Program pro = new Program();
pro.Start();
// Console.ReadKey();
}
private void Start()
{
/* 無參數用法
* //建立一個執行緒,並且傳入一個委派物件 ThreadStart,並且指向 PrintOddNumber 方法。
Thread thread1 = new Thread(new ThreadStart(PrintOddNumber));
//設定執行緒的 Name
thread1.Name = " 1 thread";
//建立一個執行緒,並且傳入一個委派物件 ThreadStart,並且指向 PrintNumber 方法。
Thread thread2 = new Thread(new ThreadStart(PrintNumber));
//設定執行緒的 Name
thread2.Name = " 2 thread";
*/
//有參數用法
Thread thread1 = new Thread(new ParameterizedThreadStart(PrintOddNumber));
//設定執行緒的 Name
thread1.Name = " 1 thread";
//建立一個執行緒,並且傳入一個委派物件 ThreadStart,並且指向 PrintNumber 方法。
Thread thread2 = new Thread(new ParameterizedThreadStart(PrintNumber));
//設定執行緒的 Name
thread2.Name = " 2 thread";
//啟動執行緒物件
thread1.Start(55);
thread2.Start(33);
}
//印出奇數
private void PrintOddNumber()
{
for (int i = 1; i < 10; i++)
{
if (i % 2 != 0)
{
Console.WriteLine("執行緒" + Thread.CurrentThread.Name + "輸出奇數" + i);
}
}
}
//印出偶數
private void PrintNumber()
{
for (int i = 1; i < 10; i++)
{
if (i % 2 == 0)
{
Console.WriteLine("執行緒" + Thread.CurrentThread.Name + "輸出偶數" + i);
}
}
}
//給予數值印出奇數
private void PrintOddNumber(object value)
{
int Number = Convert.ToInt32(value);
for (int i = 1; i < Number; i++)
{
if (i % 2 != 0)
{
Console.WriteLine("執行緒" + Thread.CurrentThread.Name + "輸出奇數" + i);
}
}
}
//給予數值印出偶數
private void PrintNumber(object value)
{
int Number = Convert.ToInt32(value);
for (int i = 1; i < Number; i++)
{
if (i % 2 == 0)
{
Console.WriteLine("執行緒" + Thread.CurrentThread.Name + "輸出偶數" + i);
}
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Threading;
//http://www.dotblogs.com.tw/atowngit/archive/2009/12/19/12547.aspx
namespace Thread_Practice
{
class Program
{
static void Main(string[] args)
{
Program pro = new Program();
pro.Start();
// Console.ReadKey();
}
private void Start()
{
/* 無參數用法
* //建立一個執行緒,並且傳入一個委派物件 ThreadStart,並且指向 PrintOddNumber 方法。
Thread thread1 = new Thread(new ThreadStart(PrintOddNumber));
//設定執行緒的 Name
thread1.Name = " 1 thread";
//建立一個執行緒,並且傳入一個委派物件 ThreadStart,並且指向 PrintNumber 方法。
Thread thread2 = new Thread(new ThreadStart(PrintNumber));
//設定執行緒的 Name
thread2.Name = " 2 thread";
*/
//有參數用法
Thread thread1 = new Thread(new ParameterizedThreadStart(PrintOddNumber));
//設定執行緒的 Name
thread1.Name = " 1 thread";
//建立一個執行緒,並且傳入一個委派物件 ThreadStart,並且指向 PrintNumber 方法。
Thread thread2 = new Thread(new ParameterizedThreadStart(PrintNumber));
//設定執行緒的 Name
thread2.Name = " 2 thread";
//啟動執行緒物件
thread1.Start(55);
thread2.Start(33);
}
//印出奇數
private void PrintOddNumber()
{
for (int i = 1; i < 10; i++)
{
if (i % 2 != 0)
{
Console.WriteLine("執行緒" + Thread.CurrentThread.Name + "輸出奇數" + i);
}
}
}
//印出偶數
private void PrintNumber()
{
for (int i = 1; i < 10; i++)
{
if (i % 2 == 0)
{
Console.WriteLine("執行緒" + Thread.CurrentThread.Name + "輸出偶數" + i);
}
}
}
//給予數值印出奇數
private void PrintOddNumber(object value)
{
int Number = Convert.ToInt32(value);
for (int i = 1; i < Number; i++)
{
if (i % 2 != 0)
{
Console.WriteLine("執行緒" + Thread.CurrentThread.Name + "輸出奇數" + i);
}
}
}
//給予數值印出偶數
private void PrintNumber(object value)
{
int Number = Convert.ToInt32(value);
for (int i = 1; i < Number; i++)
{
if (i % 2 == 0)
{
Console.WriteLine("執行緒" + Thread.CurrentThread.Name + "輸出偶數" + i);
}
}
}
}
}
留言
張貼留言