C# 多线程一一线程基础

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( );
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值