一个有趣的编程题实战----交替打印线程

题目:

有两个线程t1和t2,t1打印123,t2打印ABC,现在需要实现两个线程交替打印为"1A2B3C"

关于这道题我们可以提供多种思路来实现交替打印的结果,例如wait-notify、lock-condition ,以及Semaphore 以及yeild

先使用wait-notify

java 复制代码
public class PrintingWithWaitNotify {
    private static final Object lock = new Object();
    private static boolean printNumber = true;

    public static void main(String[] args) {
        Thread numberThread = new Thread(new NumberPrinter());
        Thread letterThread = new Thread(new LetterPrinter());

        numberThread.start();
        letterThread.start();
    }

    static class NumberPrinter implements Runnable {
        @Override
        public void run() {
            synchronized (lock) {
                try {
                    for (int i = 1; i <= 3; i++) {
                        while (!printNumber) {
                            lock.wait();
                        }
                        System.out.print(i);
                        printNumber = false;
                        lock.notify();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class LetterPrinter implements Runnable {
        @Override
        public void run() {
            synchronized (lock) {
                try {
                    for (char c = 'A'; c <= 'C'; c++) {
                        while (printNumber) {
                            lock.wait();
                        }
                        System.out.print(c);
                        printNumber = true;
                        lock.notify();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

使用lock-condition

java 复制代码
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class PrintingWithReentrant {
    public static void main(String[] args) {
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();

        Thread thread1 = new Thread(new PrintNumbers(lock, condition));
        Thread thread2 = new Thread(new PrintLetters(lock, condition));

        thread1.start();
        thread2.start();
    }
}

class PrintNumbers implements Runnable {
    private Lock lock;
    private Condition condition;

    public PrintNumbers(Lock lock, Condition condition) {
        this.lock = lock;
        this.condition = condition;
    }

    @Override
    public void run() {
        for (int i = 1; i <= 3; i++) {
            lock.lock();
            try {
                System.out.print(i);
                condition.signal(); // 唤醒打印字母的线程
                if (i < 3) {
                    condition.await(); // 等待打印字母的线程执行
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } finally {
                lock.unlock();
            }
        }
    }
}

class PrintLetters implements Runnable {
    private Lock lock;
    private Condition condition;

    public PrintLetters(Lock lock, Condition condition) {
        this.lock = lock;
        this.condition = condition;
    }

    @Override
    public void run() {
        for (char letter = 'A'; letter <= 'C'; letter++) {
            lock.lock();
            try {
                System.out.print(letter);
                condition.signal(); // 唤醒打印数字的线程
                if (letter < 'C') {
                    condition.await(); // 等待打印数字的线程执行
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } finally {
                lock.unlock();
            }
        }
    }
}

使用 Semaphore

java 复制代码
import java.util.concurrent.Semaphore;

public class PrintingWithSemaphore {
    private static final Semaphore semaphoreNumber = new Semaphore(1);
    private static final Semaphore semaphoreLetter = new Semaphore(0);

    public static void main(String[] args) {
        Thread numberThread = new Thread(new NumberPrinter());
        Thread letterThread = new Thread(new LetterPrinter());

        numberThread.start();
        letterThread.start();
    }

    static class NumberPrinter implements Runnable {
        @Override
        public void run() {
            try {
                for (int i = 1; i <= 3; i++) {
                    semaphoreNumber.acquire();
                    System.out.print(i);
                    semaphoreLetter.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    static class LetterPrinter implements Runnable {
        @Override
        public void run() {
            try {
                for (char c = 'A'; c <= 'C'; c++) {
                    semaphoreLetter.acquire();
                    System.out.print(c);
                    semaphoreNumber.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

使用yeild

java 复制代码
public class PrintingWithYeild {
    private static volatile boolean printNumber = true;

    public static void main(String[] args) {
        Thread numberThread = new Thread(new NumberPrinter());
        Thread letterThread = new Thread(new LetterPrinter());

        numberThread.start();
        letterThread.start();
    }

    static class NumberPrinter implements Runnable {
        @Override
        public void run() {
            for (int i = 1; i <= 3; i++) {
                while (!printNumber) {
                    Thread.yield();
                }
                System.out.print(i);
                printNumber = false;
                Thread.yield();
            }
        }
    }

    static class LetterPrinter implements Runnable {
        @Override
        public void run() {
            for (char c = 'A'; c <= 'C'; c++) {
                while (printNumber) {
                    Thread.yield();
                }
                System.out.print(c);
                printNumber = true;
                Thread.yield();
            }
        }
    }
}
相关推荐
武子康12 分钟前
大数据-258 离线数仓 - Griffin架构 配置安装 Livy 架构设计 解压配置 Hadoop Hive
java·大数据·数据仓库·hive·hadoop·架构
若亦_Royi13 分钟前
C++ 的大括号的用法合集
开发语言·c++
资源补给站1 小时前
大恒相机开发(2)—Python软触发调用采集图像
开发语言·python·数码相机
豪宇刘1 小时前
MyBatis的面试题以及详细解答二
java·servlet·tomcat
秋恬意1 小时前
Mybatis能执行一对一、一对多的关联查询吗?都有哪些实现方式,以及它们之间的区别
java·数据库·mybatis
m0_748247551 小时前
Web 应用项目开发全流程解析与实战经验分享
开发语言·前端·php
6.942 小时前
Scala学习记录 递归调用 练习
开发语言·学习·scala
FF在路上2 小时前
Knife4j调试实体类传参扁平化模式修改:default-flat-param-object: true
java·开发语言
真的很上进2 小时前
如何借助 Babel+TS+ESLint 构建现代 JS 工程环境?
java·前端·javascript·css·react.js·vue·html
众拾达人3 小时前
Android自动化测试实战 Java篇 主流工具 框架 脚本
android·java·开发语言