Condition和Semaphore以及Blockingqueue实现打印零与奇偶数

1.Condition实现

import org.junit.jupiter.api.Test;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class MyTest {
    private ReentrantLock lock = new ReentrantLock();
    private AtomicInteger atomic = new AtomicInteger(0);
    private Condition condition1 = lock.newCondition();
    private Condition condition2 = lock.newCondition();
    private Condition condition3 = lock.newCondition();
    private int n = 20;
    boolean flag1 = false;
    boolean flag2 = false;

    @Test
    public void test() throws InterruptedException {
        Thread t1 = new Thread(new Runnable1());
        Thread t2 = new Thread(new Runnable2());
        Thread t3 = new Thread(new Runnable3());
        t1.start();
        t2.start();
        t3.start();
        t1.join();
        t2.join();
        t3.join();
    }

    private class Runnable1 implements Runnable {
        @Override
        public void run() {
            while (atomic.get() < n) {
                try {
                    if (atomic.get() % 2 ==0) {
                        while (!flag1) {
                            Thread.yield();
                        }
                    } else {
                        while (!flag2) {
                            Thread.yield();
                        }
                    }
                    lock.lock();
                    System.out.println(0);
                    if (atomic.get() % 2 == 0) {
                        flag1 = false;
                        condition2.signal();
                    } else {
                        flag2 = false;
                        condition3.signal();
                    }
                    //这里释放锁 其他线程才能拿到
                    condition1.await();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }
    }

    private class Runnable2 implements Runnable {
        @Override
        public void run() {
            while (atomic.get() < n-1) {
                try {
                    lock.lock();
                    flag1 = true;
                    condition2.await();
                    System.out.println(atomic.incrementAndGet());
                    condition1.signal();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }
    }

    private class Runnable3 implements Runnable {
        @Override
        public void run() {
            while (atomic.get() < n) {
                try {
                    lock.lock();
                    flag2 = true;
                    condition3.await();
                    System.out.println(atomic.incrementAndGet());
                    condition1.signal();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }
    }
}

2.Semaphore实现

import org.junit.jupiter.api.Test;

import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;


public class MyTest {
    private int n = 30;
    AtomicInteger atomic = new AtomicInteger(0);
    Semaphore se1 = new Semaphore(1);
    Semaphore se2 = new Semaphore(0);
    Semaphore se3 = new Semaphore(0);

    @Test
    public void test() throws InterruptedException {
        Thread t1 = new Thread(new Runnable1());
        Thread t2 = new Thread(new Runnable2());
        Thread t3 = new Thread(new Runnable3());
        t1.start();
        t2.start();
        t3.start();
        t1.join();
        t2.join();
        t3.join();
    }

    private class Runnable1 implements Runnable {
        @Override
        public void run() {
            while (atomic.get() < n - 1) {
                try {
                    se1.acquire();
                    System.out.println(0);
                    if (atomic.get()%2 == 0) {
                        se2.release();
                    } else {
                        se3.release();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private class Runnable2 implements Runnable {
        @Override
        public void run() {
            while (atomic.get() < n - 1) {
                try {
                    se2.acquire();
                    System.out.println(atomic.incrementAndGet());
                    se1.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private class Runnable3 implements Runnable {
        @Override
        public void run() {
            while (atomic.get() < n) {
                try {
                    se3.acquire();
                    System.out.println(atomic.incrementAndGet());
                    se1.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

3.BlockingQueue实现

import org.junit.jupiter.api.Test;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;


public class MyTest {
    private int n = 30;
    AtomicInteger atomic = new AtomicInteger(0);
    LinkedBlockingQueue<Integer> q1 = new LinkedBlockingQueue<>(1);
    LinkedBlockingQueue<Integer> q2 = new LinkedBlockingQueue<>(1);
    LinkedBlockingQueue<Integer> q3 = new LinkedBlockingQueue<>(1);

    @Test
    public void test() throws InterruptedException {
        Thread t1 = new Thread(new Runnable1());
        Thread t2 = new Thread(new Runnable2());
        Thread t3 = new Thread(new Runnable3());
        t1.start();
        t2.start();
        t3.start();
        t1.join();
        t2.join();
        t3.join();
    }

    private class Runnable1 implements Runnable {
        @Override
        public void run() {
            while (atomic.get() < n - 1) {
                try {
                    q1.put(1);
                    System.out.println(0);
                    if (atomic.get()%2==0) {
                        q2.put(1);
                    } else {
                        q3.put(1);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private class Runnable2 implements Runnable {
        @Override
        public void run() {
            while (atomic.get() < n - 1){
                try {
                    q2.take();
                    System.out.println(atomic.incrementAndGet());
                    q1.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private class Runnable3 implements Runnable {
        @Override
        public void run() {
            while (atomic.get() < n) {
                try {
                    q3.take();
                    System.out.println(atomic.incrementAndGet());
                    q1.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值