优雅的写出线程t1, t2, t3分别交替输出5次 a, b, c。三种写法,a.waitnotify b.awaitsignal c.parkunpark

news/2024/5/20 9:27:39 标签: java, c++, juc, 并发编程
java">public class Test9 {
    static Object lock = new Object();

    static int num = 1;
    static int loopNum = 5;

    /**
     * 线程t1, t2, t3分别交替输出 a, b, c => 最终输出 abcabcabcabcabc
     *
     */
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            f(1, 2, "a");
        }, "t1");

        Thread t2 = new Thread(() -> {
            f(2, 3, "b");
        }, "t2");

        Thread t3 = new Thread(() -> {
            f(3, 1, "c");
        }, "t3");
        t1.start();
        t2.start();
        t3.start();
    }

    private static void f(int current, int next, String word) {
        for (int i = 0; i < loopNum; i ++) {
            synchronized (lock) {
                while (current != num) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                System.out.print(word);
                num = next;
                lock.notifyAll();
            }
        }

    }
}

java">public class Test10 {
    static ReentrantLock lock = new ReentrantLock();
    static Condition a = lock.newCondition();
    static Condition b = lock.newCondition();
    static Condition c = lock.newCondition();

    static int loopNum = 5;

    /**
     * 线程t1, t2, t3分别交替输出 a, b, c => 最终输出 abcabcabcabcabc
     */
    public static void main(String[] args) throws InterruptedException {


        Thread t1 = new Thread(() -> {
            f(a, b, "a");
        }, "t1");

        Thread t2 = new Thread(() -> {
            f(b, c, "b");
        }, "t2");

        Thread t3 = new Thread(() -> {
            f(c, a, "c");
        }, "t3");
        t1.start();
        t2.start();
        t3.start();

        Thread.sleep(1000);
        lock.lock();
        try {
            System.out.println("开始...");
            a.signal();
        } finally {
            lock.unlock();
        }
    }

    private static void f(Condition current, Condition next, String word) {
        for (int i = 0; i < loopNum; i++) {
            lock.lock();
            try {
                current.await();
                System.out.print(word);
                next.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }
}

java">public class Test11 {
    static ReentrantLock lock = new ReentrantLock();
    static Thread t1;
    static Thread t2;
    static Thread t3;

    static int loopNum = 5;

    /**
     * 线程t1, t2, t3分别交替输出 a, b, c => 最终输出 abcabcabcabcabc
     */
    public static void main(String[] args) throws InterruptedException {

        t1 = new Thread(() -> {
            f(t2, "a");
        }, "t1");

        t2 = new Thread(() -> {
            f(t3, "b");
        }, "t2");

        t3 = new Thread(() -> {
            f(t1, "c");
        }, "t3");
        t1.start();
        t2.start();
        t3.start();

        Thread.sleep(1000);
        LockSupport.unpark(t1);

    }

    private static void f(Thread next, String word) {
        for (int i = 0; i < loopNum; i ++) {
            LockSupport.park();
            System.out.print(word);
            LockSupport.unpark(next);
        }
    }
}

http://www.niftyadmin.cn/n/1326909.html

相关文章

win10升级助手_Win10自带杀毒软件如此强大,大家却不爱用,究竟是为什么呢?...

前段时间有位用户问小电&#xff0c;装了win10系统之后&#xff0c;还有必要安装其他杀毒软件吗&#xff1f;相信很多小伙伴把电脑系统升级到win10后&#xff0c;大都会有这样的疑问。那么&#xff0c;我们是否有必要安装其他杀毒软件呢&#xff1f;小电答曰&#xff1a;“否”…

double-checked-locking,双重检测,单例模式

public class Singleton {private Singleton() {}private static volatile Singleton SINGLETON null;public static Singleton getInstance() {// 只有实例没创建的时候&#xff0c;才会进入synchronized代码块if (SINGLETON null) {synchronized (Singleton.class) {// 也许…

SQL数据库不用SQL语句能显示全表的内容_100道MySQL数据库经典面试题解析

1. MySQL索引使用有哪些注意事项呢&#xff1f;可以从三个维度回答这个问题&#xff1a;索引哪些情况会失效&#xff0c;索引不适合哪些场景&#xff0c;索引规则索引哪些情况会失效查询条件包含or&#xff0c;可能导致索引失效如何字段类型是字符串&#xff0c;where时一定用引…

自己手写一个简易版本的数据库连接池【Java】【详细的注解,有思考过程和知识点】

public class Test14 {public static void main(String[] args) {ConnectionPool pool new ConnectionPool(3);for (int i 0; i < 4; i) {new Thread(() -> {Connection connection pool.take();// 模拟使用的时间try {Thread.sleep(1000);} catch (InterruptedExcept…

自己手写一个简易版本的线程池【Java】【详细注解,含思考过程和知识点】

Slf4j public class Test15 {public static void main(String[] args) {ThreadPool pool new ThreadPool(1, 1000, TimeUnit.MILLISECONDS, 1, (taskQueue, task) -> {// 这里实现自己的拒绝策略// 死等 // taskQueue.put(task);// 带超时时间的阻塞添加taskQue…

XHR如何爬虫_程序员如何炼成 Python 爬虫“王者”?

作者 | 周萝卜责编 | 郭芮出品 | CSDN(ID&#xff1a;CSDNnews)本文章精选了五个爬虫实例&#xff0c;希望能够给想要入门 Python 爬虫的小伙伴儿们一些帮助。网易精选评价爬取首先来看一个网易精选网站的爬虫例子&#xff0c;可以爬取评价的商品很多&#xff0c;这里选择“iPh…

python网络爬虫_Python连载(一):网络爬虫基础及pythpon环境搭建

原标题&#xff1a;Python连载(一)&#xff1a;网络爬虫基础及pythpon环境搭建从今天开始&#xff0c;我们的Python连载正式开始啦&#xff5e;接下来我们会给大家分享Python网络爬虫的相关技术课程。一、我们先来了解下什么是网络爬虫&#xff1f;网络爬虫又被称为网页蜘蛛、网…

js数组获取index_浅浅谈vue.js

1、Vue的生命周期在Vue官方文档中生命周期函数有如下定义每个 Vue 实例在被创建时都要经过一系列的初始化过程——例如&#xff0c;需要设置数据监听、编译模板、将实例挂载到 DOM 并在数据变化时更新 DOM 等。同时在这个过程中也会运行一些叫做生命周期钩子的函数&#xff0c;…