Java程序之多线程顺序打印 ABC

发布于:2024-06-24 ⋅ 阅读:(131) ⋅ 点赞:(0)
题目:

        按顺序打印 ABC ABC ABC ...。有这么一个多线程场景问题:有三个线程,线程1执行(输出A)完成之后线程2执行(输出B),线程2执行完之后线程3执行(输出C),线程3执行完成之后线程1执行...,整体循环50次,写程序实现

算法思路:

要求按照顺序打印,即A、B、C轮流打印,每个字母打印一次后换下一个字母继续打印,循环进行。

        1. 创建一个名为ThreadThreadp的类,其中包含一个flag变量,用于标记当前应该打印哪个字母。初始值为0,表示先打印A。
        2. 在ThreadThreadp类中定义三个同步方法printa()、printb()和printc(),分别用于打印A、B、C字母。这三个方法都需要传入一个ThreadThreadp对象作为参数。
        3. 在printa()方法中,首先判断flag是否为0,如果是,则打印A字母,并将flag设置为1,然后唤醒其他等待的线程。接着调用wait()方法让当前线程进入等待状态。
        4. 在printb()方法中,首先判断flag是否为1,如果是,则打印B字母,并将flag设置为2,然后唤醒其他等待的线程。接着调用wait()方法让当前线程进入等待状态。
        5. 在printc()方法中,首先判断flag是否为2,如果是,则打印C字母,并让当前线程休眠1秒,然后将flag设置为0,最后唤醒其他等待的线程。接着调用wait()方法让当前线程进入等待状态。
        6. 在main方法中,创建ThreadThreadp对象t,以及三个PrintA、PrintB、PrintC对象,分别传入t作为参数。然后创建三个线程t1、t2、t3,分别执行这三个PrintA、PrintB、PrintC对象的run()方法。最后启动这三个线程。
        7. 运行程序,可以看到按照顺序打印出ABC三个字母。

源代码:
Info.java
package Question9;

public class Info {
    private int flag = 0;
    public synchronized void printa() throws InterruptedException {
        while (true)
        {
            if(flag ==0)
            {
                System.out.println("A");
                flag = 1;
                notifyAll();
            }
            wait();
        }
    }
    public synchronized   void printb() throws InterruptedException {
        while (true)
        {
            if(flag ==1)
            {
                System.out.println("B");
                flag = 2;
                notifyAll();
            }
            wait();
        }
    }
    public synchronized void printc() throws InterruptedException {
        while (true) {
            if (flag == 2) {
                System.out.println("C");
                Thread.sleep(1000);
                flag = 0;
                notifyAll();
            }
            wait();
        }
    }

}

PrintA.java
package Question9;

class PrintA implements Runnable {
    private Info info;

    PrintA(Info info) {
        this.info = info;
    }

    @Override
    public void run() {
        try {
            info.printa();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
PrintB.java
package Question9;

class PrintB implements Runnable {

    private Info t;

    PrintB(Info t) {
        this.t = t;
    }

    @Override
    public void run() {
        try {
            t.printb();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
PrintC.java
package Question9;

class PrintC implements Runnable {
    private Info t;

    PrintC(Info t) {
        this.t = t;
    }

    @Override
    public void run() {
        try {
            t.printc();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Mian.java
package Question9;

public class Mian {
    public static void main(String[]args) throws InterruptedException
    {
        Info t = new Info();
        PrintA printA = new PrintA(t);
        PrintB printB = new PrintB(t);
        PrintC printC = new PrintC(t);
        Thread t1 = new Thread(printA);
        Thread t2 = new Thread(printB);
        Thread t3 = new Thread(printC);
        t1.start();
        t2.start();
        t3.start();

    }
}
运行结果: 


网站公告

今日签到

点亮在社区的每一天
去签到