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

多线程实现的交替打印的多种实现形式。说白了就是使用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();
    }
}

猜你喜欢

转载自blog.csdn.net/Yoke______/article/details/124297658