步步为营-64-进程&线程

1 进程

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 进程线程
{
class Program
{
static void Main(string[] args)
{
//获取当前进程
Process p1 = Process.GetCurrentProcess();
Console.WriteLine(p1.Id);
Console.WriteLine(p1.ProcessName); //打开新的进程
Process p2 = Process.Start("cmd.exe");
string key = Console.ReadLine();
if (key=="k")
{
//杀死进程
p2.Kill();
}
Console.ReadLine(); }
}
}

步步为营-64-进程&线程

2 应用程序域

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 应用程序域
{
class Program
{
static void Main(string[] args)
{
//01 获取当前应用程序域
AppDomain ad = AppDomain.CurrentDomain;
Console.WriteLine(ad.FriendlyName);
//02 在当前程序域中打开程序集,不会开启新进程
AppDomain ap2= AppDomain.CreateDomain("xyxtl");
int id = ap2.ExecuteAssembly("进程线程.exe");
Console.Write(id); Console.ReadKey();
}
}
}

步步为营-64-进程&线程

3 线程

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace 线程
{
class Program
{
static void Main(string[] args)
{
#region 01 获取当前线程-默认程序启动后,会有一个主线程
Thread t1 = Thread.CurrentThread;
Console.WriteLine(t1.ManagedThreadId);
#endregion #region 02 开辟一个新线程 - 02-01 无参;02-02 有参
//02-01 有参
Thread t2 = new Thread(() =>
{
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);//输出当前线程编号
Console.WriteLine("无参,构造函数!");
});
t2.Start(); //02-02 有参
#endregion Thread t3 = new Thread((p) =>
{
//由于参数是object类型,如果想访问对象特有成员,需要进行类型转换
Person p2 = p as Person;
if (p2!=null)
{
;
Console.WriteLine(p2.Age);
} Console.WriteLine(p.ToString());
});
t3.Start(new Person()
{
Name = "张三",
Age = ,
});
Console.Read();
} public class Person
{
public string Name { get; set; }
public int Age { get; set; } public override string ToString()
{
return string.Format("姓名:{0},年龄{1}",Name,Age);
}
}
}
}

步步为营-64-进程&线程

3.2 IsBackground属性

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms; namespace 前台线程与后台线程
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Thread t1 = new Thread(() =>
{
while (true)
{
Console.WriteLine();
}
});
//休息5秒
// Thread.Sleep(5000); t1.Start();
} private void button2_Click(object sender, EventArgs e)
{
Thread t1 = new Thread(() =>
{
while (true)
{
Console.WriteLine();
}
});
t1.IsBackground = true;
t1.Start();
}
}
}

当线程是后台线程时,主线程关闭,后台线程也随之关闭;
当线程是前台线程时,主线程关闭,前台线程不关闭;

3.3 join 属性,阻塞join代码所在的当前线程==插队

步步为营-64-进程&线程

步步为营-64-进程&线程

4 lock

问题的引出

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace Lock锁
{
class Program
{
static void Main(string[] args)
{
int num = ; Thread th = new Thread(() =>
{
for (int i = ; i < ; i++)
{
num--;
}
});
th.IsBackground = true;
th.Start(); for (int i = ; i < ; i++)
{
num++;
} Console.WriteLine(num);
Console.ReadKey();
}
}
}

步步为营-64-进程&线程

解决方法:加锁

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace Lock锁
{
class Program
{
static void Main(string[] args)
{
int num = ; Thread th = new Thread(() =>
{
for (int i = ; i < ; i++)
{
lock ("B")
{
num --;
}
}
});
th.IsBackground = true;
th.Start(); for (int i = ; i < ; i++)
{
lock ("B")
{
num ++;
}
} Console.WriteLine(num);
Console.ReadKey();
}
}
}

步步为营-64-进程&线程

5 栈 stack

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 栈
{
class Program
{
static void Main(string[] args)
{
//定义栈
Stack<BaoZi> bzStack = new Stack<BaoZi>( );
//入栈
bzStack.Push(new BaoZi());
//出栈
if (bzStack.Count>)
{
BaoZi bz= bzStack.Pop();
} } public class BaoZi
{
}
}
}

6 线程池

  线程池中的线程都是后台线程

  不能手动设置每个线程的属性(前台,优先级)

  比较短的任务考虑线程池,比较长的任务考虑手动创建一个线程

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace 线程池
{
class Program
{
static void Main(string[] args)
{
for (int i = ; i < ; i++)
{
ThreadPool.QueueUserWorkItem((obj) =>
{
Console.WriteLine(obj+"_"+Thread.CurrentThread.ManagedThreadId);
Thread.Sleep();
},i
);
}
Console.Read();
}
}
}

步步为营-64-进程&线程

7 异步方式调用委托对象

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace 异步方式调用委托对象
{
class Program
{
static void Main(string[] args)
{
//定义一个委托
Action<string> s1 = (s) =>
{
Console.WriteLine(s+"_"+ Thread.CurrentThread.ManagedThreadId);
};
//委托的实现-01
//s1("张三");
//委托的实现-02 异步调用实现委托
s1.BeginInvoke("张三",Func1,""); //获取主线程id
Console.WriteLine(Thread.CurrentThread.ManagedThreadId); Console.ReadKey();
} #region 委托实现后的回调函数
private static void Func1(IAsyncResult ar)
{
Console.WriteLine("李四"+"_"+Thread.CurrentThread.ManagedThreadId);
}
#endregion
}
}

步步为营-64-进程&线程

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace 异步方式调用委托对象
{
class Program
{
static void Main(string[] args)
{
//定义一个委托
Action<string> s1 = (s) =>
{
Console.WriteLine(s+"_"+ Thread.CurrentThread.ManagedThreadId);
};
//委托的实现-01
//s1("张三");
//委托的实现-02 异步调用实现委托
IAsyncResult result = s1.BeginInvoke("张三",Func1,"");
//保证委托执行完成后,执行后续代码
//只保证委托执行完成,不保证回调函数也执行完成
s1.EndInvoke(result); //获取主线程id
Console.WriteLine(Thread.CurrentThread.ManagedThreadId); Console.ReadKey();
} #region 委托实现后的回调函数
private static void Func1(IAsyncResult ar)
{
Console.WriteLine("李四"+"_"+Thread.CurrentThread.ManagedThreadId);
}
#endregion
}
}

步步为营-64-进程&线程

8 并行计算

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 并行计算
{
class Program
{
static void Main(string[] args)
{
Stopwatch sp = new Stopwatch();
sp.Start();
//普通计算
//for (int i = 0; i < 100000; i++)
//{
// Console.WriteLine(i);
//}
//并行计算
Parallel.For(1, 100000, (i) => { Console.WriteLine(i); });
sp.Stop();
Console.WriteLine(sp.Elapsed);
Console.ReadKey();
}
}
}

步步为营-64-进程&线程

上一篇:01构建第一个SpringBoot工程


下一篇:《ZLToolKit源码学习笔记》(9)线程模块之任务执行器