通过反射,破坏单例设计模式
internal class Program
{
static void Main(string[] args)
{
//懒汉式
//for (int i = 0; i < 10; i++)
//{
// Thread th = new Thread(() => {
// LazyManClass lazyManClass = LazyManClass.GetLazyMan();
// });
// th.Start();
//}
//通过反射,破解单例设计模式
//Assembly(获取要反射的程序集DLL--->Class Method INterface Delegate Event。。。。) Type
Type type = typeof(LazyManClass);
ConstructorInfo[] ci = type.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance);
//正常的调用静态方法创建对象
//LazyManClass lazyManClass = LazyManClass.GetLazyMan();
//ci[0]就是私有的构造函数
//通过调用私有的构造函数,跳过判断,直接创建对象
LazyManClass l1 = (LazyManClass)ci[0].Invoke(null);
//在第二次创建对象之前,获取b的值,重新修改为false
FieldInfo fi = type.GetField("b", BindingFlags.NonPublic | BindingFlags.Static);
//修改字段的值
//参数1:要修改的字段
//参数2:修改后的值
fi.SetValue(fi, false);
LazyManClass l2 = (LazyManClass)ci[0].Invoke(null);
fi.SetValue(fi, false);
LazyManClass l3 = (LazyManClass)ci[0].Invoke(null);
fi.SetValue(fi, false);
LazyManClass l4 = (LazyManClass)ci[0].Invoke(null);
Console.WriteLine(l1.GetHashCode());
Console.WriteLine(l2.GetHashCode());
Console.WriteLine(l3.GetHashCode());
Console.WriteLine(l4.GetHashCode());
Console.ReadKey();
}
}
class LazyManClass
{
private LazyManClass()
{
if (b == false)
{
b = true;
}
else
{
throw new Exception("有人破坏单例设计模式!!!!!");
}
//if (_lazyManClass != null)
//{
// throw new Exception("有人破坏单例设计模式!!!!!");
//}
Console.WriteLine("我被创建了一次");
}
private static LazyManClass _lazyManClass = null;
private static object o = new object();
private static bool b = false;
public static LazyManClass GetLazyMan()
{
if (_lazyManClass == null)
{
lock (o)
{
if (_lazyManClass == null)
{
return _lazyManClass = new LazyManClass();
}
}
}
return _lazyManClass;
}
}
基于Task的异步编程模型
internal class Program
{
static async Task Main(string[] args)
{
//以同步的方式,开发异步的代码
//下载一个网页的数据---->写入到指定的路径下
//异步会传染
//异步方法的返回值类型必须放到Task<T>中 ,如果没有返回值,则写Task---->void Task<int> int
Console.WriteLine("Main函数调用异步方法之前的线程" + Thread.CurrentThread.ManagedThreadId);
int r = await DownHTML_WriteFile("http://www.taobao.com", @"C:\Users\ThinkPad\Desktop\abcabc.txt");
Console.WriteLine("Main函数调用异步方法之后的线程" + Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("数据的长度是{0}", r);
Task 底层就是线程池
//Task task = new Task(() =>
//{
// Console.WriteLine("开始了一个任务");
//});
//task.ContinueWith((t) =>
//{
// Console.WriteLine("我是任务1");
// Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
//}).ContinueWith((t) =>
//{
// Console.WriteLine("我是任务2");
// Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
//}).ContinueWith((t) => {
// Console.WriteLine("我是任务3");
// Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
//});
//task.Start();
//Task.Run(() =>
//{
// Console.WriteLine("开始了一个任务");
//});
//Task.Factory.StartNew(() => {
// Console.WriteLine("开始了一个任务");
//});
//任务:I/O密集型 Input/OutPut------>写入一个文件 读取一个文件 读取一个数据 写入一个数据库
//任务: 计算密集型
Console.ReadKey();
}
//Task:异步执行的任务
//async 异步方法 [MehodImp(。。。。。sync....)]
//await 等待异步方法执行后的结果
static async Task<int> DownHTML_WriteFile(string url, string path)
{
//状态机设计模式
WebClient wc = new WebClient();
Console.WriteLine("开始异步任务1之前的线程是" + Thread.CurrentThread.ManagedThreadId);
//DownloadStringTaskAsync :基于任务的异步方法
Task<