多线程按序打印leetcode1114

该博客介绍了如何使用Java实现多线程间的同步,通过一个`Foo`类的`first`、`second`和`third`方法,确保线程按特定顺序执行。博客中使用了`volatile`关键字和`synchronized`块来控制并发访问,确保线程安全地依次打印'first'、'second'和'third'。
摘要由CSDN通过智能技术生成
package com.springboot.leetcode;


/**
 * @author Created by leo_args.
 * @date 2020/12/8
 *
 * 主要思路是使用成员变量,作为多线程间的排序因子
 * 排序因子可选项比较多 int Atomic…… Queue CountDownLatch Stack Semaphore 等
 */
class Foo {

    private volatile int flag = 1;

    public Foo() {

    }

    public void first(Runnable printFirst) throws InterruptedException {

        // printFirst.run() outputs "first". Do not change or remove this line.
        printFirst.run();
        synchronized (this){
            flag++;
        }
    }

    public void second(Runnable printSecond) throws InterruptedException {

        // printSecond.run() outputs "second". Do not change or remove this line.
        for (;;){
            if(flag == 2){
                break;
            }
        }
        printSecond.run();
        synchronized (this){
            flag++;
        }
    }

    public void third(Runnable printThird) throws InterruptedException {

        // printThird.run() outputs "third". Do not change or remove this line.
        for (;;){
            if(flag == 3){
                break;
            }
        }
        printThird.run();
        synchronized (this){
            flag++;
        }
    }

    public static void main(String[] args) {

        Foo foo = new Foo();

        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    foo.second(new Runnable() {
                        @Override
                        public void run() {
                            System.out.print("second");
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    foo.first(new Runnable() {
                        @Override
                        public void run() {
                            System.out.print("first");
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread threadC = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    foo.third(new Runnable() {
                        @Override
                        public void run() {
                            System.out.print("third");
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        threadA.start();
        threadB.start();
        threadC.start();
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值