1. 线程创建,暂停,终止
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Recipe1
{
class Program
{
static void PrintNumbers( )
{
Console.WriteLine( "Starting program..." );
for( int i = 1; i < 10; i++ )
{
Console.WriteLine( i );
}
}
static void PrintNumbersWithDelay( )
{
Console.WriteLine( "Starting thread..." );
for( int i = 1; i < 10; i++ )
{
Thread.Sleep( TimeSpan.FromSeconds(2));
Console.WriteLine( i );
}
}
static void Main( string[ ] args )
{
//线程创建
Thread t = new Thread( PrintNumbersWithDelay );
t.Start( );
//线程暂停
Thread.Sleep( TimeSpan.FromSeconds( 6 ) );
//线程终止
t.Abort( );
Console.WriteLine( "A thread has been aborted" );
Thread t1 = new Thread( PrintNumbers );
t1.Start( );
PrintNumbers( );
Console.ReadKey( );
}
}
}
运行结果:
2. 线程等待
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Recipe1
{
class Program
{
static void PrintNumbers( )
{
Console.Write( "Starting program..." );
for( int i = 1; i < 10; i++ )
{
Console.WriteLine( i );
}
}
static void PrintNumbersWithDelay( )
{
Console.WriteLine( "Starting thread..." );
for( int i = 1; i < 10; i++ )
{
Thread.Sleep( TimeSpan.FromSeconds(2));
Console.WriteLine( i );
}
}
static void Main( string[ ] args )
{
Thread t = new Thread( PrintNumbersWithDelay );
t.Start( );
//线程等待
t.Join( );
Console.WriteLine( "Thread Completed" );
Console.ReadKey( );
}
}
}
运行结果:
3. 检测线程状态
Console.WriteLine( Thread.CurrentThread.ThreadState.ToString( ) );
4. 线程优先级
t.Priority = ThreadPriority.Highest; // 或Lowest
5. 前台线程和后台线程
t.IsBackground = true;
6. 向线程传递参数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace recipe8
{
class Program
{
class ThreadSample
{
private readonly int _iterations;
public ThreadSample( int iterations )
{
_iterations = iterations;
}
public void CountNumbers( )
{
for( int i = 1; i <= _iterations; i++ )
{
Thread.Sleep( TimeSpan.FromSeconds( 0.5 ) );
Console.WriteLine( "{0} prints {1}",
Thread.CurrentThread.Name, i );
}
}
}
static void Main( string[ ] args )
{
var Sample = new ThreadSample( 10 );
var ThreadOne = new Thread( Sample.CountNumbers );
ThreadOne.Name = "ThreadOne";
ThreadOne.Start( );
ThreadOne.Join( );
Console.WriteLine( "--------------------" );
Console.ReadKey( );
}
}
}
7. lock关键字
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace recipe9
{
class Program
{
abstract class CounterBase
{
public abstract void Increment( );
public abstract void Decrement( );
}
static void TestCounter( CounterBase c)
{
for( int i = 0; i < 10000; i++ )
{
c.Increment( );
c.Decrement( );
}
}
class Counter : CounterBase
{
public int Count
{
get;
private set;
}
public override void Increment( )
{
Count++;
}
public override void Decrement( )
{
Count--;
}
}
class CounterWithLock : CounterBase
{
private readonly object _syncRoot = new Object( );
public int Count
{
get;
private set;
}
public override void Increment( )
{
lock(_syncRoot)
Count++;
}
public override void Decrement( )
{
lock( _syncRoot )
Count--;
}
}
static void Main( string[ ] args )
{
Console.WriteLine( "Incorrect counter" );
var c = new Counter( );
var t1 = new Thread( ( ) => TestCounter( c ) );
var t2 = new Thread( ( ) => TestCounter( c ) );
var t3 = new Thread( ( ) => TestCounter( c ) );
t1.Start( );
t2.Start( );
t3.Start( );
t1.Join( );
t2.Join( );
t3.Join( );
Console.WriteLine( "Total Count: {0}", c.Count );
var c1 = new CounterWithLock( );
var t11 = new Thread( ( ) => TestCounter( c1 ) );
var t21 = new Thread( ( ) => TestCounter( c1 ) );
var t31 = new Thread( ( ) => TestCounter( c1 ) );
t11.Start( );
t21.Start( );
t31.Start( );
t11.Join( );
t21.Join( );
t31.Join( );
Console.WriteLine( "Total Count: {0}", c1.Count );
Console.ReadKey( );
}
}
}