Java轮流打印字符


两个线程轮流打印A、B

public class ThreadDemo {

    public static void main(String[] args) {
        Thread t1 = new TT("A");
        Thread t2 = new TT("B");
        t1.start();
        t2.start();
    }

}

class TT extends Thread {
    private static Object LOCK = new Object();
    private String mName;
    private boolean flag;

    public TT() {
    }

    public TT(String name) {
        this.mName = name;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            synchronized (LOCK) {
                System.out.println(mName);
                flag = !flag;
                LOCK.notify();
                if (i < 10) {
                    try {
                        LOCK.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }// end of synchronized
        }// end for
    }// end run
}


三个线程轮流打印 A、B、C


import java.util.concurrent.atomic.AtomicInteger;

public class TestAsynTreadXunlei {
    public static void main(String argv[]) {

        AtomicInteger synObj = new AtomicInteger(0);

        TestPrint a = new TestPrint(synObj, "A", 0);
        TestPrint b = new TestPrint(synObj, "B", 1);
        TestPrint c = new TestPrint(synObj, "C", 2);

        a.start();
        b.start();
        c.start();
    }
}

class TestPrint extends Thread {

    private AtomicInteger synObj;
    private String name;
    private int flag;

    private int count = 0;

    public TestPrint(AtomicInteger synObj, String name, int flag) {
        this.synObj = synObj;
        this.name = name;
        this.flag = flag;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (synObj) {
                if (synObj.get() % 3 == flag) {
                    synObj.set(synObj.get() + 1);
                    System.out.println(name);
                    count++;
                    synObj.notifyAll();
                    if (count == 10) {
                        break;
                    }
                } else {
                    try {
                        synObj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}



以下是一种可能的实现: ```java public class PrintABC { public static void main(String[] args) { Printer printer = new Printer(); Thread threadA = new Thread(new PrintTask(printer, &#39;a&#39;), "Thread A"); Thread threadB = new Thread(new PrintTask(printer, &#39;b&#39;), "Thread B"); Thread threadC = new Thread(new PrintTask(printer, &#39;c&#39;), "Thread C"); threadA.start(); threadB.start(); threadC.start(); } private static class Printer { private char currentChar = &#39;a&#39;; private int count = 0; public synchronized void printChar(char c) { while (c != currentChar) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.print(c); currentChar = (char) (&#39;a&#39; + (currentChar - &#39;a&#39; + 1) % 3); count++; if (count == 30) { // 打印完十次后结束程序 System.exit(0); } notifyAll(); } } private static class PrintTask implements Runnable { private final Printer printer; private final char c; public PrintTask(Printer printer, char c) { this.printer = printer; this.c = c; } @Override public void run() { for (int i = 0; i < 10; i++) { printer.printChar(c); } } } } ``` 该程序中,`Printer`类维护了当前应该打印字符`currentChar`和已经打印的次数`count`。`printChar`方法是线程安全的,每个线程在调用该方法时会等待当前应该打印字符是自己的时候才打印,否则就一直等待。打印完一个字符后,`currentChar`会更新为下一个应该打印字符,并且会唤醒所有等待的线程(因为下一个应该打印字符可能是其他线程对应的字符)。 在`main`方法中创建了三个`PrintTask`对象,并将它们分别封装在三个线程中启动。每个`PrintTask`对象负责打印对应的字符打印十次后就结束。 整个程序的关键在于`Printer`类中的`printChar`方法,它保证了每个线程都能按照规定的顺序打印自己的字符
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值