[C#基础]线程学习笔记(一)

参考链接:http://www.cnblogs.com/miniwiki/archive/2010/06/18/1760540.html

msdn上的相关介绍(Thread类):https://msdn.microsoft.com/zh-cn/library/system.threading.thread(v=vs.110).aspx


1.创建一个简单的线程

using System;
using System.Threading;

class ThreadTest
{
    static void Main(string[] args)
    {
        Thread t = new Thread(WriteY);
        t.Start();
        while (true)
        {
            Console.Write("x");
            Console.Read();
        }
    }

    static void WriteY()
    {
        while (true)
        {
            Console.Write("y");
            Console.Read();
        }
    }
}

2.数据共享

之所以是"较大几率",是因为对象t和线程这两个东东的执行先后顺序是不确定的,这就涉及到线程安全的问题了

using System;
using System.Threading;

class ThreadTest2
{
    bool done = false;

    static void Main(string[] args)
    {
        ThreadTest2 t = new ThreadTest2();
        new Thread(t.Go).Start();
        t.Go();
        Console.Read();
    }

    void Go()
    {
        if (!done)
        {
            //a.输出一次(调用的对象都是t)
            done = true;
            Console.Write("done");

            //b.输出两次(较大几率)
            //Console.Write("done");
            //done = true;
        }
    }
}

3.锁

保证线程安全的一个方法就是设置锁,当两个线程争夺一个锁的时候,先争夺到的线程会先进入,而另一个线程则会等待

锁可以限制线程进入临界区

using System;
using System.Threading;

class ThreadTest3
{
    bool done = false;
    object locker = new object();

    static void Main(string[] args)
    {
        ThreadTest3 t = new ThreadTest3();
        new Thread(t.Go).Start();
        t.Go();
        Console.Read();
    }

    void Go()
    {
        lock (locker)
        {
            if (!done)
            {
                //a.输出一次
                Console.Write("done");
                done = true;
            }
        }
    }
}

4.线程临时暂停

也是保证线程安全的一个方法

using System;
using System.Threading;

class ThreadTest4
{
    static void Main(string[] args)
    {
        Thread.Sleep(TimeSpan.FromSeconds(3));//3秒后输出
        ThreadTest4 t = new ThreadTest4();
        new Thread(t.Go).Start();
        t.Go();
        Console.Read();
    }

    void Go()
    {
        Console.Write("done");
    }
}


5.线程阻塞

using System;
using System.Threading;

class ThreadTest5
{
    static void Main(string[] args)
    {
        Thread t = new Thread(Go);
        t.Join();//阻塞线程,将不会输出并提示异常
        t.Start();
    }

    static void Go()
    {
        Console.Write("hello");
    }
}


6.与线程相关的委托

区别是一个能传递参数,一个不能

public delegate void ThreadStart()
public delegate void ParameterizedThreadStart(
	Object obj
)

using System;
using System.Threading;

class ThreadTest6
{
    static void Main(string[] args)
    {
        //ThreadStart是.net中的委托
        Thread t = new Thread(new ThreadStart(Go));
        t.Start();

        //匿名委托
        Thread t2 = new Thread(delegate() { Console.Write("world"); });
        t2.Start();

        Console.Read();
    }

    static void Go()
    {
        Console.Write("hello");
    }
}

7.与线程相关的委托

using System;
using System.Threading;

class ThreadTest7
{
    static void Main(string[] args)
    {
        Thread t = new Thread(Go);
        t.Start(true);//Start的重载方法,只能传递一个参数
        Go(false);

        //传递多个参数
        Thread t2 = new Thread(delegate() { Go(1, 2); });
        t2.Start();

        Console.Read();
    }

    static void Go(object o)
    {
        bool b = (bool)o;
        Console.Write(b ? "hello" : "world");
    }

    static void Go(int a, int b)
    {
        Console.Write(a + b);
    }
}

8.使用匿名委托的注意点

using System;
using System.Threading;

class ThreadTest8
{
    static void Main(string[] args)
    {
        string text = "Before";
        Thread t = new Thread(delegate() { WriteText(text); });
        text = "After";
        t.Start();//输出After
        Console.Read();
    }

    static void WriteText(string text)
    {
        Console.Write(text);
    }
}

9.将实例方法传入线程

using System;
using System.Threading;

class ThreadTest9
{
    bool b = false;

    static void Main(string[] args)
    {
        ThreadTest9 instance = new ThreadTest9();
        instance.b = true;
        Thread t = new Thread(instance.Go);
        t.Start();

        ThreadTest9 instance2 = new ThreadTest9();
        instance2.Go();

        Console.Read();
    }

    void Go()
    {
        Console.Write(b ? "hello" : "world");
    }
}

10.命名线程

using System;
using System.Threading;

class ThreadTest10
{
    static void Main(string[] args)
    {
        Thread.CurrentThread.Name = "main";
        Thread t = new Thread(Go);
        t.Name = "t";
        t.Start();
        Go();
        Console.Read();
    }

    static void Go()
    {
        Console.WriteLine("hello from " + Thread.CurrentThread.Name);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值