大家好,又见面了,我是你们的朋友全栈君。
ManualResetEvent类
#作用
一个线程通知其他一个或多个线程正在发送的事件。
ManualResetEvent manualResetEvent = new ManualResetEvent(false);
参数表示调用WaitOne()方法的线程是继续执行(true)还是阻塞(false)。
WaitOne():调用WaitOne()方法的线程会阻塞,等待主控线程的manualResetEvent.Set()信号。
Set():将事件状态设为true,调用WaitOne()的线程继续执行。
Reset():将事件状态设为false,调用WaitOne()的线程阻塞。
Set() Reset():只针对WaitOne()方法,不影响WaitOne()方法之前的程序执行。若没有信号,调用WaitOne()方法的线程一直阻塞。
1.事件初始状态设为false,task线程在第一个WaitOne()处阻塞。
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ManualResetEvent测试
{
class Program
{
static ManualResetEvent manualResetEvent = new ManualResetEvent(false);
static void Main(string[] args)
{
//Task线程和主线程同时执行
Task task = Task.Factory.StartNew(() =>
{
GetDataFromServer(1);
});
Console.ReadLine();
}
static void GetDataFromServer(int serverNumber)
{
Console.WriteLine("I get first data from server" + serverNumber);
manualResetEvent.WaitOne();
Console.WriteLine("I get second data from server" + serverNumber);
Thread.Sleep(TimeSpan.FromSeconds(2));
Console.WriteLine("I get third data from server" + serverNumber);
manualResetEvent.WaitOne();
Console.WriteLine("All the data collected from server" + serverNumber);
}
}
}
输出:
I get first data from server1
2.manualResetEvent.Set()事件状态设为true,task线程在每一个WaitOne()处都不阻塞。
namespace ManualResetEvent测试
{
class Program
{
static ManualResetEvent manualResetEvent = new ManualResetEvent(false);
static void Main(string[] args)
{
//两个Task线程和主线程同时执行
Task task = Task.Factory.StartNew(() =>
{
GetDataFromServer(1);
});
//主线程执行,将信号set为true,调用WaitOne()的线程被释放
manualResetEvent.Set();
Console.ReadLine();
}
static void GetDataFromServer(int serverNumber)
{
Console.WriteLine("I get first data from server" + serverNumber);
manualResetEvent.WaitOne();
Console.WriteLine("I get second data from server" + serverNumber);
Thread.Sleep(TimeSpan.FromSeconds(2));
Console.WriteLine("I get third data from server" + serverNumber);
manualResetEvent.WaitOne();
Console.WriteLine("All the data collected from server" + serverNumber);
}
}
}
输出:
I get first data from server1
I get second data from server1
I get third data from server1
All the data collected from server1
3.manualResetEvent调用Set()再调用Reset(),task线程在第一个WaitOne()处阻塞。
namespace ManualResetEvent测试
{
class Program
{
static ManualResetEvent manualResetEvent = new ManualResetEvent(false);
static void Main(string[] args)
{
//两个Task线程和主线程同时执行
Task task = Task.Factory.StartNew(() =>
{
GetDataFromServer(1);
});
//主线程执行,调用WaitOne()的线程被释放再马上阻塞
manualResetEvent.Set();
manualResetEvent.Reset();
Console.ReadLine();
}
static void GetDataFromServer(int serverNumber)
{
Console.WriteLine("I get first data from server" + serverNumber);
manualResetEvent.WaitOne();
Console.WriteLine("I get second data from server" + serverNumber);
Thread.Sleep(TimeSpan.FromSeconds(2));
Console.WriteLine("I get third data from server" + serverNumber);
manualResetEvent.WaitOne();
Console.WriteLine("All the data collected from server" + serverNumber);
}
}
}
输出:
I get first data from server1
4.manualResetEvent.Set()事件状态设为true,task线程在第一个WaitOne()处阻塞然后被释放。
namespace ManualResetEvent测试
{
class Program
{
static ManualResetEvent manualResetEvent = new ManualResetEvent(false);
static void Main(string[] args)
{
//Task线程和主线程同时执行
Task task = Task.Factory.StartNew(() =>
{
GetDataFromServer(1);
});
setEvent.Set();
manualResetEvent.Reset();
Thread.Sleep(TimeSpan.FromSeconds(8));
manualResetEvent.Set();
Console.ReadLine();
}
static void GetDataFromServer(int serverNumber)
{
Console.WriteLine("I get first data from server" + serverNumber);
manualResetEvent.WaitOne();
Console.WriteLine("I get second data from server" + serverNumber);
Thread.Sleep(TimeSpan.FromSeconds(2));
Console.WriteLine("I get third data from server" + serverNumber);
manualResetEvent.WaitOne();
Console.WriteLine("All the data collected from server" + serverNumber);
}
}
}
输出:
先输出 I get first data from server1
然后task被阻塞,8秒后task获得Set()信号,再输出 I get second data from server1
再间隔2秒输出,第二个WaitOne()不会被阻塞
I get third data from server1
All the data collected from server1
5.三个线程异步执行,set()释放所有线程
namespace ManualResetEvent测试
{
class Program
{
static ManualResetEvent manualResetEvent = new ManualResetEvent(false);
static void Main(string[] args)
{
//两个Task线程和主线程同时执行
Task task = Task.Factory.StartNew(() =>
{
GetDataFromServer(1);
});
Task.Factory.StartNew(() =>
{
GetDataFromServer(2);
});
manualResetEvent.Set();
manualResetEvent.Reset();
Thread.Sleep(TimeSpan.FromSeconds(8));
manualResetEvent.Set();
Console.ReadLine();
}
static void GetDataFromServer(int serverNumber)
{
Console.WriteLine("I get first data from server" + serverNumber);
manualResetEvent.WaitOne();
Console.WriteLine("I get second data from server" + serverNumber);
Thread.Sleep(TimeSpan.FromSeconds(2));
Console.WriteLine("I get third data from server" + serverNumber);
manualResetEvent.WaitOne();
Console.WriteLine("All the data collected from server" + serverNumber);
}
}
}
输出:
先输出
I get first data from server1
I get first data from server2
然后task被阻塞,8秒后task获得Set()信号,再输出
I get second data from server2
I get second data from server1
再间隔2秒输出,第二个WaitOne()不会被阻塞
I get third data from server2
All the data collected from server2
I get third data from server1
All the data collected from server1
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/161173.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...