循环打印ABC

这里写自定义目录标题

package com.wly;

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

public class LockMethod {
    private static int state = 0;
    private static ReentrantLock lock = new ReentrantLock();
    static class ThreadA extends Thread {
        @Override
        public void run() {
            for (int i=1; i<=100;) {
                try{
                    lock.lock();
                    while (state % 3 ==0) {
                        System.out.println("A");
                        state++;
                        i++;
                    }
                }finally {
                    lock.unlock();
                }
            }
        }
    }

    static class ThreadB extends Thread {
        @Override
        public void run() {
            for (int i=1; i<=100;) {
                try{
                    lock.lock();
                    while (state % 3 ==1) {
                        System.out.println("B");
                        state++;
                        i++;
                    }
                }finally {
                    lock.unlock();
                }
            }
        }
    }

    static class ThreadC extends Thread {
        @Override
        public void run() {
            for (int i=1; i<=100;) {
                try{
                    lock.lock();
                    while (state % 3 ==2) {
                        System.out.println("C");
                        state++;
                        i++;
                    }
                }finally {
                    lock.unlock();
                }
            }
        }
    }
    public static void main(String[] args) {
        new ThreadA().start();
        new ThreadB().start();
        new ThreadC().start();
    }
}

信号量

public class Main {
    public static Semaphore semaphoreA = new Semaphore(1);
    public static Semaphore semaphoreB = new Semaphore(0);
    public static Semaphore semaphoreC = new Semaphore(0);
    static class ThreadA extends Thread{
        @Override
        public void run() {
            try{
                for(int i=1; i<=100; i++) {
                    semaphoreA.acquire();
                    System.out.println("A");
                    semaphoreB.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
    static class ThreadB extends Thread {
        @Override
        public void run() {
            try{
                for(int i=1; i<=100; i++) {
                    semaphoreB.acquire();
                    System.out.println("B");
                    semaphoreC.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    static class ThreadC extends Thread {
        @Override
        public void run() {
            try{
                for(int i=1; i<=100; i++) {
                    semaphoreC.acquire();
                    System.out.println("C");
                    semaphoreA.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        new ThreadA().start();
        new ThreadB().start();
        new ThreadC().start();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值