Thread类可以创建和控制线程
最简单的例子
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsolTread
{
class Program
{
static void Main()
{
Thread t1 = new Thread(ThreadMain);
t1.Start();
Console.WriteLine("this is main thread");
}
static void ThreadMain()
{
Console.WriteLine("Running in a thread");
}
}
}
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsolTread
{
class Program
{
static void Main()
{
Thread t1 = new Thread(ThreadMain);
t1.Start();
Console.WriteLine("this is main thread");
}
static void ThreadMain()
{
Console.WriteLine("Running in a thread");
}
}
}
给线程传送数据一般有两种方法:
第一种方法:
public struct Data
{
public string Message;
}
static void Main()
{
Thread t1 = new Thread(ThreadMain);
t1.Name = "first";
Data d = new Data();
d.Message = "123";
t1.Start(d);
Console.WriteLine("this is main thread");
}
static void ThreadMain(object o)
{
Data d=(Data)o;
Console.WriteLine("Running in a thread{0},Id:{1},Message:{2}",Thread.CurrentThread.Name,Thread.CurrentThread.ManagedThreadId,d.Message);
}
public string Message;
}
static void Main()
{
Thread t1 = new Thread(ThreadMain);
t1.Name = "first";
Data d = new Data();
d.Message = "123";
t1.Start(d);
Console.WriteLine("this is main thread");
}
static void ThreadMain(object o)
{
Data d=(Data)o;
Console.WriteLine("Running in a thread{0},Id:{1},Message:{2}",Thread.CurrentThread.Name,Thread.CurrentThread.ManagedThreadId,d.Message);
}
另外一种创建一个定制 类
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsolTread
{
class Program
{
static void Main()
{
MyThread obj = new MyThread("obj");
Thread t1 = new Thread(obj.ThreadMain);
t1.Start();
}
public class MyThread
{
private string data;
public MyThread(string data)
{
this.data = data;
}
public void ThreadMain()
{
Console.WriteLine("Running in a thread{0},Id:{1},Message:{2}", Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId, data);
}
}
}
}
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsolTread
{
class Program
{
static void Main()
{
MyThread obj = new MyThread("obj");
Thread t1 = new Thread(obj.ThreadMain);
t1.Start();
}
public class MyThread
{
private string data;
public MyThread(string data)
{
this.data = data;
}
public void ThreadMain()
{
Console.WriteLine("Running in a thread{0},Id:{1},Message:{2}", Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId, data);
}
}
}
}
解决竞争条件方法:第一种是可以使用lock锁定共享资源如下
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsolTread
{
class Program
{//共享资源类
public class StateObject
{
private int state=5;
public void ChangeState(int loop)
{
if(state==5)
{
state++;
Trace.Assert(state == 6, "Race condintion occurred after" + loop + "Loop");
}
state = 5;
}
}
public class SampleThread
{
public void RaceCondition(object o)
{
Trace.Assert(o is StateObject, "o must be of type StateObject");
StateObject state = o as StateObject;
int i = 0;
while (true)
{
lock (state)//共享资源锁定
{
state.ChangeState(i++);
}
}
}
}
static void Main()
{
StateObject state = new StateObject();
for (int i = 0; i < 20;i++)
{
new Thread(new SampleThread().RaceCondition).Start(state);
}
}
}
}
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsolTread
{
class Program
{//共享资源类
public class StateObject
{
private int state=5;
public void ChangeState(int loop)
{
if(state==5)
{
state++;
Trace.Assert(state == 6, "Race condintion occurred after" + loop + "Loop");
}
state = 5;
}
}
public class SampleThread
{
public void RaceCondition(object o)
{
Trace.Assert(o is StateObject, "o must be of type StateObject");
StateObject state = o as StateObject;
int i = 0;
while (true)
{
lock (state)//共享资源锁定
{
state.ChangeState(i++);
}
}
}
}
static void Main()
{
StateObject state = new StateObject();
for (int i = 0; i < 20;i++)
{
new Thread(new SampleThread().RaceCondition).Start(state);
}
}
}
}
第二种是将共享对象设置为线程完全的对象也就是在上面的ChangeState()方法包含一个lock语句
public class StateObject{
private int state=5;
private object sync = new object();
public void ChangeState(int loop)
{
lock(sync)//每次修改state都来锁定sync对象
{
if(state==5)
{
state++;
Trace.Assert(state == 6, "Race condintion occurred after" + loop + "Loop");
}
state = 5;
}
}
}
private object sync = new object();
public void ChangeState(int loop)
{
lock(sync)//每次修改state都来锁定sync对象
{
if(state==5)
{
state++;
Trace.Assert(state == 6, "Race condintion occurred after" + loop + "Loop");
}
state = 5;
}
}
}
线程同步
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsolTread
{
class Program
{
//设置一个共享状态
public class SharedState
{
public int state { get; set; }
}
public class Task
{
SharedState shareState;
public Task(SharedState shareState)
{
this.shareState = shareState;
}
public void DoTheTask()
{
for (int i = 0; i < 5000;i++ )
{
lock(shareState)////////
{
shareState.state += 1;
}
}
}
}
static void Main()
{
int numThreads = 20;
SharedState state = new SharedState();
Thread[] threads = new Thread[numThreads];
for (int i = 0; i < numThreads;i++ )
{
threads[i] = new Thread(new Task(state).DoTheTask);
threads[i].Start();
}
for (int i = 0; i < numThreads; i++)
{
threads[i].Join();
}
Console.WriteLine("sum {0}",state.state);
}
}
}
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsolTread
{
class Program
{
//设置一个共享状态
public class SharedState
{
public int state { get; set; }
}
public class Task
{
SharedState shareState;
public Task(SharedState shareState)
{
this.shareState = shareState;
}
public void DoTheTask()
{
for (int i = 0; i < 5000;i++ )
{
lock(shareState)////////
{
shareState.state += 1;
}
}
}
}
static void Main()
{
int numThreads = 20;
SharedState state = new SharedState();
Thread[] threads = new Thread[numThreads];
for (int i = 0; i < numThreads;i++ )
{
threads[i] = new Thread(new Task(state).DoTheTask);
threads[i].Start();
}
for (int i = 0; i < numThreads; i++)
{
threads[i].Join();
}
Console.WriteLine("sum {0}",state.state);
}
}
}
浙公网安备 33010602011771号