5、多线程-按顺序调用,A->B->C,AA 打印 5 次,BB 打印10 次,CC 打印 15 次,重复 10 次
1.1、提取两个字符串中的公共部分2.2、在字符串“abc123def456”中,如何新的内存空间中获取到字符串“abcdef3.C#冒泡排序算法4.C#选择排序(Selection Sort)算法5.C#插入排序算法6.C#希尔排序算法7.C#归并排序算法8.C#快速排序算法9.C#堆排序算法10.C#计数排序算法11.C#桶排序算法12.C#基数排序算法13.C#二分查找算法14.C#线性查找算法15.C#二叉搜索树算法16.C#哈希查找算法17.C# 面试常见递归算法18.C#经典算法面试题19.程序设计,委托时间的应用20.1、多线程-打印零与奇偶数21.2、多线程-三个线程分别打印 A,B,C,要求这三个线程一起运行,打印 n 次,输出形如“ABCABCABC....”的字符串22.3、多线程-两个线程交替打印 0~100 的奇偶数23.4、多线程-通过 N 个线程顺序循环打印从 0 至 100
24.5、多线程-按顺序调用,A->B->C,AA 打印 5 次,BB 打印10 次,CC 打印 15 次,重复 10 次
25.6、多线程 - 用两个线程,一个输出字母,一个输出数字,交替输出 1A2B3C4D...26Z题目
多线程按顺序调用,A->B->C,AA 打印 5 次,BB 打印10 次,CC 打印 15 次,重复 10 次
代码示例
using System;
using System.Threading;
using System.Threading.Tasks;
public class ABCPrinter
{
private int repeatCount;
private int aPrintCount;
private int bPrintCount;
private int cPrintCount;
private AutoResetEvent aEvent = new AutoResetEvent(true); // 一开始 A 可以运行
private AutoResetEvent bEvent = new AutoResetEvent(false);
private AutoResetEvent cEvent = new AutoResetEvent(false);
public ABCPrinter(int repeatCount)
{
this.repeatCount = repeatCount;
this.aPrintCount = 5;
this.bPrintCount = 10;
this.cPrintCount = 15;
}
public void PrintA(Action<string> printChar)
{
for (int i = 0; i < repeatCount; i++)
{
for (int j = 0; j < aPrintCount; j++)
{
aEvent.WaitOne(); // 等待 A 事件
printChar("A"); // 打印 A
bEvent.Set(); // 唤醒 B 线程
}
}
}
public void PrintB(Action<string> printChar)
{
for (int i = 0; i < repeatCount; i++)
{
for (int j = 0; j < bPrintCount; j++)
{
bEvent.WaitOne(); // 等待 B 事件
printChar("B"); // 打印 B
cEvent.Set(); // 唤醒 C 线程
}
}
}
public void PrintC(Action<string> printChar)
{
for (int i = 0; i < repeatCount; i++)
{
for (int j = 0; j < cPrintCount; j++)
{
cEvent.WaitOne(); // 等待 C 事件
printChar("C"); // 打印 C
if (j == cPrintCount - 1)
{
aEvent.Set(); // 最后一个 C 的时候唤醒 A 线程
}
else
{
cEvent.Set(); // 继续唤醒 C 线程
}
}
}
}
public static void Main(string[] args)
{
int repeatCount = 10;
ABCPrinter abcPrinter = new ABCPrinter(repeatCount);
var aTask = Task.Run(() => abcPrinter.PrintA(Console.Write));
var bTask = Task.Run(() => abcPrinter.PrintB(Console.Write));
var cTask = Task.Run(() => abcPrinter.PrintC(Console.Write));
Task.WaitAll(aTask, bTask, cTask);
Console.WriteLine();
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)