多线程实现的交替打印的多种实现形式

多线程实现的交替打印的多种实现形式。说白了就是使用java‘中的各种同步器实现这个交替打印的需求。

sync的实现方式

package com.AQS.JiaoTiDaYin;

/**
 * @Author:XK
 * @Date: Created in 12:00 2022/4/20
 * @Description:多线程交替打印的多种实现方法 (sync)
 **/
public class JiaotiDayinSync {
    private static int counter=0;
    private static Object object= new Object();
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                int cnt = 0;
                while (cnt<3){
                    synchronized (object){
                        if(counter%3!=0){
                            try {
                                object.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }else {
                            System.out.println("1");
                            cnt++;
                            counter++;
                        }
                        object.notifyAll();
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                int cnt = 0;
                while (cnt<3){
                    synchronized (object){
                        if(counter%3!=1){
                            try {
                                object.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }else {
                            System.out.println("2");
                            cnt++;
                            counter++;
                        }
                        object.notifyAll();
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                int cnt = 0;
                while (cnt<3){
                    synchronized (object){
                        if(counter%3!=2){
                            try {
                                object.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }else {
                            System.out.println("3");
                            cnt++;
                            counter++;
                        }
                        object.notifyAll();
                    }
                }
            }
        }).start();
    }
}

lock类 ReentrantLock的实现方式

package com.AQS.JiaoTiDaYin;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author:XK
 * @Date: Created in 12:10 2022/4/20
 * @Description:lock实现  ReentrantLock实现
 **/
public class Lock123 {
    private static int state = 0;
    private static Lock lock= new ReentrantLock();

    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 3;) {
                    lock.lock();
                    try {
                        while (state%3==0){
                            System.out.println(1);
                            state++;
                            i++;
                        }
                    }finally {
                        lock.unlock();
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 3;) {
                    lock.lock();
                    try {
                        while (state%3==1){
                            System.out.println(2);
                            state++;
                            i++;
                        }
                    }finally {
                        lock.unlock();
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 3;) {
                    lock.lock();
                    try {
                        while (state%3==2){
                            System.out.println(3);
                            state++;
                            i++;
                        }
                    }finally {
                        lock.unlock();
                    }
                }
            }
        }).start();
    }
}

semaphore信号量实现

package com.AQS.JiaoTiDaYin;

import java.util.concurrent.Semaphore;

/**
 * @Author:XK
 * @Date: Created in 14:41 2022/4/20
 * @Description:
 **/
public class Sema123 {
    private static Semaphore A = new Semaphore(1);
    private static Semaphore B = new Semaphore(0);
    private static Semaphore C = new Semaphore(0);

    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 3; i++) {
                    try {
                        A.acquire();
                        System.out.println(1);
                        B.release();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 3; i++) {
                    try {
                        B.acquire();
                        System.out.println(2);
                        C.release();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 3; i++) {
                    try {
                        C.acquire();
                        System.out.println(3);
                        A.release();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值