三个线程分别打印 A,B,C,要求这三个线程一起运行,打印 n 次,输出形如“ABCABCABC....”的字符串

发布时间 2023-03-29 23:43:27作者: elegydance
using System;
using System.Threading;

class PrintThread
{
    private string text;
    private int count;
    private Semaphore semaphore;
    private Semaphore nextSemaphore;

    public PrintThread(string text, int count, Semaphore semaphore, Semaphore nextSemaphore)
    {
        this.text = text;
        this.count = count;
        this.semaphore = semaphore;
        this.nextSemaphore = nextSemaphore;
    }

    public void Run()
    {
        for (int i = 0; i < count; i++)
        {
            semaphore.WaitOne();  // 等待信号量
            Console.Write(text);
            nextSemaphore.Release();  // 释放下一个线程的信号量
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        Semaphore semaphoreA = new Semaphore(1, 1);
        Semaphore semaphoreB = new Semaphore(0, 1);
        Semaphore semaphoreC = new Semaphore(0, 1);

        int n = 10;  // 打印 n 次
        PrintThread threadA = new PrintThread("A", n, semaphoreA, semaphoreB);
        PrintThread threadB = new PrintThread("B", n, semaphoreB, semaphoreC);
        PrintThread threadC = new PrintThread("C", n, semaphoreC, semaphoreA);

        Thread t1 = new Thread(threadA.Run);
        Thread t2 = new Thread(threadB.Run);
        Thread t3 = new Thread(threadC.Run);

        t1.Start();
        t2.Start();
        t3.Start();

        t1.Join();
        t2.Join();
        t3.Join();
    }
}

这段代码中,我们使用了信号量(Semaphore)来控制线程的执行顺序。在主程序中,我们创建了三个信号量semaphoreAsemaphoreBsemaphoreC,分别用于控制线程 A、B、C 的执行顺序。

然后,我们创建了三个PrintThread对象,分别表示打印 A、B、C 的线程。每个线程的构造函数中,都传入了它自己的信号量semaphore和下一个线程的信号量nextSemaphore

在每个线程的Run方法中,首先等待自己的信号量,表示它需要等待前一个线程完成打印。然后,它打印自己的字符,并释放下一个线程的信号量,表示下一个线程可以开始打印。

在主程序中,我们创建了三个Thread对象t1t2t3,分别对应线程 A、B、C,然后启动这三个线程并等待它们执行完毕。最终输出的结果是形如“ABCABCABC....”的字符串,共打印了 n 次。