①多线程可以提高CPU的利用率,因为当一个线程处于等待状态的时候,CPU会去执行另外的线程;
②提高了CPU的利用率,就可以直接提高程序的整体执行速度;
①线程开的越多,内存占用越大
②协调和管理代码的难度加大,需要CPU时间跟踪线程
③线程之间对资源的共享可能会产生可不遇知的问题
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace sss
{
class Program
{
//主线程
static void Main(string[] args)
{
Thread thread = new Thread(ThreadMethod); //执行的必须是无返回值的方法
thread.Name = "子线程";
//thread.Start("王建"); //方法内传递参数,类型为object,发送和接收涉及到拆装箱操作
thread.Start();
Console.ReadKey();
}
public static void ThreadMethod(object parameter) //方法内可以有参数,也可以没有参数
{
Console.WriteLine("{0}开始执行。", Thread.CurrentThread.Name);
}
}
}
说明:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace sss
{
class Program
{
//主线程
static void Main(string[] args)
{
Thread thread = new Thread(ThreadMethod);//执行的必须是无返回值的方法
thread.Name = "子线程";
thread.Start();
StringBuilder threadInfo = new StringBuilder();
threadInfo.Append(" 线程当前的执行状态: " + thread.IsAlive);
threadInfo.Append("\n 线程当前的名字: " + thread.Name);
threadInfo.Append("\n 线程当前的优先级: " + thread.Priority);
threadInfo.Append("\n 线程当前的状态: " + thread.ThreadState);
Console.Write(threadInfo);
Console.ReadKey();
}
public static void ThreadMethod(object parameter)
{
Console.WriteLine("{0}开始执行。", Thread.CurrentThread.Name);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace sss
{
class Program
{
static void Main(string[] args)
{
Thread thread = new Thread(ThreadMethod); //执行的必须是无返回值的方法
thread.Name = "小A";
thread.Start();
Console.ReadKey();
}
public static void ThreadMethod(object parameter)
{
Console.WriteLine("我是:{0},我要终止了", Thread.CurrentThread.Name);
//开始终止线程
Thread.CurrentThread.Abort();
//下面的代码不会执行
for (int i = 0; i < 10; i++)
{
Console.WriteLine("我是:{0},我循环{1}次", Thread.CurrentThread.Name, i);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace sss
{
class Program
{
static void Main(string[] args)
{
Thread thread = new Thread(ThreadMethod);//执行的必须是无返回值的方法
thread.Name = "小A";
thread.Start();
Console.ReadKey();
}
public static void ThreadMethod(object parameter)
{
try
{
Console.WriteLine("我是:{0},我要终止了", Thread.CurrentThread.Name);
//开始终止线程
Thread.CurrentThread.Abort();
}
catch (ThreadAbortException ex)
{
Console.WriteLine("我是:{0},我又恢复了", Thread.CurrentThread.Name);
//恢复被终止的线程
Thread.ResetAbort();
}
for (int i = 0; i < 10; i++)
{
Console.WriteLine("我是:{0},我循环{1}次", Thread.CurrentThread.Name, i);
}
}
}
}
public static void Sleep(TimeSpan timeout); //时间段
public static void Sleep(int millisecondsTimeout); //毫秒数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace sss
{
class Program
{
static void Main(string[] args)
{
Thread threadA = new Thread(ThreadMethod); //执行的必须是无返回值的方法
threadA.Name = "小A";
threadA.Start();
Console.ReadKey();
}
public static void ThreadMethod(object parameter)
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("我是:{0},我循环{1}次", Thread.CurrentThread.Name, i);
Thread.Sleep(300); //休眠300毫秒
}
}
}
}
public void Join();
public bool Join(int millisecondsTimeout); //毫秒数
public bool Join(TimeSpan timeout); //时间段
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace sss
{
class Program
{
static void Main(string[] args)
{
Thread threadA = new Thread(ThreadMethod); //执行的必须是无返回值的方法
threadA.Name = "小A";
Thread threadB = new Thread(ThreadMethod); //执行的必须是无返回值的方法
threadB.Name = "小B";
threadA.Start();
//threadA.Join();
threadB.Start();
//threadB.Join();
for (int i = 0; i < 10; i++)
{
Console.WriteLine("我是:主线程,我循环{1}次", Thread.CurrentThread.Name, i);
Thread.Sleep(300); //休眠300毫秒
}
Console.ReadKey();
}
public static void ThreadMethod(object parameter)
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("我是:{0},我循环{1}次", Thread.CurrentThread.Name, i);
Thread.Sleep(300); //休眠300毫秒
}
}
}
}
static void Main(string[] args)
{
Thread threadA = new Thread(ThreadMethod); //执行的必须是无返回值的方法
threadA.Name = "小A";
threadA.Start();
Thread.Sleep(3000); //休眠3000毫秒
threadA.Resume(); //继续执行已经挂起的线程
Console.ReadKey();
}
public static void ThreadMethod(object parameter)
{
Thread.CurrentThread.Suspend(); //挂起当前线程
for (int i = 0; i < 10; i++)
{
Console.WriteLine("我是:{0},我循环{1}次", Thread.CurrentThread.Name, i);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace sss
{
class Program
{
static void Main(string[] args)
{
Thread threadA = new Thread(ThreadMethod); //执行的必须是无返回值的方法
threadA.Name = "A";
Thread threadB = new Thread(ThreadMethod); //执行的必须是无返回值的方法
threadB.Name = "B";
threadA.Priority = ThreadPriority.Highest;
threadB.Priority = ThreadPriority.BelowNormal;
threadB.Start();
threadA.Start();
Thread.CurrentThread.Name = "C";
ThreadMethod(new object());
Console.ReadKey();
}
public static void ThreadMethod(object parameter)
{
for (int i = 0; i < 500; i++)
{
Console.Write(Thread.CurrentThread.Name);
}
}
}
}
public class Singleton
{
private static Singleton instance;
private Singleton() //私有函数,防止实例
{
}
public static Singleton GetInstance()
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
public class Singleton
{
private static Singleton instance;
private static object obj=new object();
private Singleton() //私有化构造函数
{
}
public static Singleton GetInstance()
{
if(instance==null)
{
lock(obj) //通过Lock关键字实现同步
{
if(instance==null)
{
instance=new Singleton();
}
}
}
return instance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace sss
{
class Program
{
static void Main(string[] args)
{
Thread threadA = new Thread(ThreadMethod); //执行的必须是无返回值的方法
threadA.Name = "王文建";
Thread threadB = new Thread(ThreadMethod); //执行的必须是无返回值的方法
threadB.Name = "生旭鹏";
threadA.Start();
threadB.Start();
Console.ReadKey();
}
public static void ThreadMethod(object parameter)
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("我是:{0},我循环{1}次", Thread.CurrentThread.Name, i);
Thread.Sleep(300);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace sss
{
class Program
{
static void Main(string[] args)
{
Program pro = new Program();
Thread threadA = new Thread(pro.ThreadMethod); //执行的必须是无返回值的方法
threadA.Name = "王文建";
Thread threadB = new Thread(pro.ThreadMethod); //执行的必须是无返回值的方法
threadB.Name = "生旭鹏";
threadA.Start();
threadB.Start();
Console.ReadKey();
}
public void ThreadMethod(object parameter)
{
lock (this) //添加lock关键字
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("我是:{0},我循环{1}次", Thread.CurrentThread.Name, i);
Thread.Sleep(300);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace sss
{
class Program
{
static void Main(string[] args)
{
Program pro1 = new Program();
Program pro2 = new Program();
Thread threadA = new Thread(pro1.ThreadMethod); //执行的必须是无返回值的方法
threadA.Name = "王文建";
Thread threadB = new Thread(pro2.ThreadMethod); //执行的必须是无返回值的方法
threadB.Name = "生旭鹏";
threadA.Start();
threadB.Start();
Console.ReadKey();
}
public void ThreadMethod(object parameter)
{
lock (this)
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("我是:{0},我循环{1}次", Thread.CurrentThread.Name, i);
Thread.Sleep(300);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace sss
{
class Program
{
private static object obj = new object();
static void Main(string[] args)
{
Program pro1 = new Program();
Program pro2 = new Program();
Thread threadA = new Thread(pro1.ThreadMethod); //执行的必须是无返回值的方法
threadA.Name = "王文建";
Thread threadB = new Thread(pro2.ThreadMethod); //执行的必须是无返回值的方法
threadB.Name = "生旭鹏";
threadA.Start();
threadB.Start();
Console.ReadKey();
}
public void ThreadMethod(object parameter)
{
lock (obj)
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("我是:{0},我循环{1}次", Thread.CurrentThread.Name, i);
Thread.Sleep(300);
}
}
}
}
}
static void Main(string[] args)
{
Thread threadA = new Thread(ThreadMethod); //执行的必须是无返回值的方法
threadA.Name = "A";
Thread threadB = new Thread(ThreadMethod); //执行的必须是无返回值的方法
threadB.Name = "B";
threadA.Start();
threadB.Start();
Thread.CurrentThread.Name = "C";
ThreadMethod();
Console.ReadKey();
}
static object obj = new object();
public static void ThreadMethod()
{
Monitor.Enter(obj); //Monitor.Enter(obj) 锁定对象
try
{
for (int i = 0; i < 500; i++)
{
Console.Write(Thread.CurrentThread.Name);
}
}
catch(Exception ex){ }
finally
{
Monitor.Exit(obj); //释放对象
}
}
static void Main(string[] args)
{
Thread threadA = new Thread(ThreadMethod); //执行的必须是无返回值的方法
threadA.Name = "A";
Thread threadB = new Thread(ThreadMethod); //执行的必须是无返回值的方法
threadB.Name = "B";
threadA.Start();
threadB.Start();
Thread.CurrentThread.Name = "C";
ThreadMethod();
Console.ReadKey();
}
static object obj = new object();
public static void ThreadMethod()
{
bool flag = Monitor.TryEnter(obj, 1000); //设置1S的超时时间,如果在1S之内没有获得同步锁,则返回false
//上面的代码设置了锁定超时时间为1秒,也就是说,在1秒中后,
//lockObj还未被解锁,TryEntry方法就会返回false,如果在1秒之内,lockObj被解锁,TryEntry返回true。我们可以使用这种方法来避免死锁
try
{
if (flag)
{
for (int i = 0; i < 500; i++)
{
Console.Write(Thread.CurrentThread.Name);
}
}
}
catch(Exception ex)
{
}
finally
{
if (flag)
Monitor.Exit(obj);
}
}
///
/// 怪物类
///
internal class Monster
{
public int Blood { get; set; }
public Monster(int blood)
{
this.Blood = blood;
Console.WriteLine("我是怪物,我有{0}滴血",blood);
}
}
///
/// 攻击类
///
internal class Play
{
///
/// 攻击者名字
///
public string Name { get; set; }
///
/// 攻击力
///
public int Power{ get; set; }
///
/// 法术攻击
///
public void magicExecute(object monster)
{
Monster m = monster as Monster;
Monitor.Enter(monster);
while (m.Blood>0)
{
Monitor.Wait(monster);
Console.WriteLine("当前英雄:{0},正在使用法术攻击打击怪物", this.Name);
if(m.Blood>= Power)
{
m.Blood -= Power;
}
else
{
m.Blood = 0;
}
Thread.Sleep(300);
Console.WriteLine("怪物的血量还剩下{0}", m.Blood);
Monitor.PulseAll(monster);
}
Monitor.Exit(monster);
}
///
/// 物理攻击
///
///
public void physicsExecute(object monster)
{
Monster m = monster as Monster;
Monitor.Enter(monster);
while (m.Blood > 0)
{
Monitor.PulseAll(monster);
if (Monitor.Wait(monster, 1000)) //非常关键的一句代码
{
Console.WriteLine("当前英雄:{0},正在使用物理攻击打击怪物", this.Name);
if (m.Blood >= Power)
{
m.Blood -= Power;
}
else
{
m.Blood = 0;
}
Thread.Sleep(300);
Console.WriteLine("怪物的血量还剩下{0}", m.Blood);
}
}
Monitor.Exit(monster);
}
}
static void Main(string[] args)
{
//怪物类
Monster monster = new Monster(1000);
//物理攻击类
Play play1 = new Play() { Name = "无敌剑圣", Power = 100 };
//魔法攻击类
Play play2 = new Play() { Name = "流浪法师", Power = 120 };
Thread thread_first = new Thread(play1.physicsExecute); //物理攻击线程
Thread thread_second = new Thread(play2.magicExecute); //魔法攻击线程
thread_first.Start(monster);
thread_second.Start(monster);
Console.ReadKey();
}
static void Main(string[] args)
{
Thread[] thread = new Thread[3];
for (int i = 0; i < 3; i++)
{
thread[i] = new Thread(ThreadMethod1);
thread[i].Name = i.ToString();
}
for (int i = 0; i < 3; i++)
{
thread[i].Start();
}
Console.ReadKey();
}
public static void ThreadMethod1(object val)
{
mutet.WaitOne(); //获取锁
for (int i = 0; i < 500; i++)
{
Console.Write(Thread.CurrentThread.Name);
}
mutet.ReleaseMutex(); //释放锁
}
static void Main(string[] args)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadMethod1), new object()); //参数可选
Console.ReadKey();
}
public static void ThreadMethod1(object val)
{
for (int i = 0; i <= 500000000; i++)
{
if (i % 1000000 == 0)
{
Console.Write(Thread.CurrentThread.Name);
}
}
}
1. http://www.cnblogs.com/JeffreyZhao/archive/2009/07/22/thread-pool-1-the-goal-and-the-clr-thread-pool.html
2. https://www.cnblogs.com/wwj1992/p/5976096.html