多线程交替打印数字

多线程交替打印数字,第1张

多线程交替打印数字

 CyclicBarrier 

    private final AtomicInteger count = new AtomicInteger(0);

    private final CyclicBarrier cyclicBarrier = new CyclicBarrier(3);

    private volatile int state;

    public void first() throws InterruptedException {
        for (int i = 0; i < 15; i++) {
            if (state == 0) {
                for (int j = 0; j < 5; j++) {
                    System.out.println(
                            Thread.currentThread().getName() + "-" + count.incrementAndGet());
                }
                state = 1;
            }
            try {
                cyclicBarrier.await();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
    }

    public void second() throws InterruptedException {
        for (int i = 0; i < 15; i++) {
            if (state == 1) {
                for (int j = 0; j < 5; j++) {
                    System.out.println(
                            Thread.currentThread().getName() + "-" + count.incrementAndGet());
                }
                state = 2;
            }
            try {
                cyclicBarrier.await();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
    }

 

  Thread.yield()

    private final AtomicInteger count = new AtomicInteger(0);

    private volatile int state;

    public void first() {
        for (int i = 0; i < 5; i++) {
            while (state != 0) {
                Thread.yield();
            }
            for (int j = 0; j < 5; j++) {
                System.out.println(
                        Thread.currentThread().getName() + "-" + count.incrementAndGet());
            }
            state = 1;
        }
    }

    public void second() {
        for (int i = 0; i < 5; i++) {
            while (state != 1) {
                Thread.yield();
            }
            for (int j = 0; j < 5; j++) {
                System.out.println(
                        Thread.currentThread().getName() + "-" + count.incrementAndGet());
            }
            state = 2;
        }
    }

    public void third() {
        for (int i = 0; i < 5; i++) {
            while (state != 2) {
                Thread.yield();
            }
            for (int j = 0; j < 5; j++) {
                System.out.println(
                        Thread.currentThread().getName() + "-" + count.incrementAndGet());
            }
            state = 0;
        }
    }

 Semaphore 

    private final AtomicInteger count = new AtomicInteger(0);

    private final Semaphore first = new Semaphore(1);
    private final Semaphore second = new Semaphore(0);
    private final Semaphore third = new Semaphore(0);

    public void first() throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            first.acquire();
            for (int j = 0; j < 5; j++) {
                System.out.println(
                        Thread.currentThread().getName() + "-" + count.incrementAndGet());
            }
            second.release();
        }
    }

    public void second() throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            second.acquire();
            for (int j = 0; j < 5; j++) {
                System.out.println(
                        Thread.currentThread().getName() + "-" + count.incrementAndGet());
            }
            third.release();
        }
    }

    public void third() throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            third.acquire();
            for (int j = 0; j < 5; j++) {
                System.out.println(
                        Thread.currentThread().getName() + "-" + count.incrementAndGet());
            }
            first.release();
        }

 CountDownLatch

    private final AtomicInteger count = new AtomicInteger(0);

    private CountDownLatch first = new CountDownLatch(0);
    private CountDownLatch second = new CountDownLatch(1);
    private CountDownLatch third = new CountDownLatch(1);

    public void first() throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            first.await();
            for (int j = 0; j < 5; j++) {
                System.out.println(
                        Thread.currentThread().getName() + "-" + count.incrementAndGet());
            }
            first = new CountDownLatch(1);
            second.countDown();
        }
    }

    public void second() throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            second.await();
            for (int j = 0; j < 5; j++) {
                System.out.println(
                        Thread.currentThread().getName() + "-" + count.incrementAndGet());
            }
            second = new CountDownLatch(1);
            third.countDown();
        }
    }

    public void third() throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            third.await();
            for (int j = 0; j < 5; j++) {
                System.out.println(
                        Thread.currentThread().getName() + "-" + count.incrementAndGet());
            }
            third = new CountDownLatch(1);
            first.countDown();
        }
    }

 LockSupport

    private final AtomicInteger count = new AtomicInteger(0);

    private Map map = new ConcurrentHashMap<>(4);

    private volatile int state;

    public void first() throws InterruptedException {
        map.put("first", Thread.currentThread());
        for (int i = 0; i < 5; i++) {
            while (state != 0) {
                LockSupport.park();
            }
            for (int j = 0; j < 5; j++) {
                System.out.println(
                        Thread.currentThread().getName() + "-" + count.incrementAndGet());
            }
            state = 1;
            LockSupport.unpark(map.get("second"));
        }
    }

    public void second() throws InterruptedException {
        map.put("second", Thread.currentThread());
        for (int i = 0; i < 5; i++) {
            while (state != 1) {
                LockSupport.park();
            }
            for (int j = 0; j < 5; j++) {
                System.out.println(
                        Thread.currentThread().getName() + "-" + count.incrementAndGet());
            }
            state = 2;
            LockSupport.unpark(map.get("third"));
        }
    }

    public void third() throws InterruptedException {
        map.put("third", Thread.currentThread());
        for (int i = 0; i < 5; i++) {
            while (state != 2) {
                LockSupport.park();
            }
            for (int j = 0; j < 5; j++) {
                System.out.println(
                        Thread.currentThread().getName() + "-" + count.incrementAndGet());
            }
            state = 0;
            LockSupport.unpark(map.get("first"));
        }
    }
ReentrantLock + Condition
    private final AtomicInteger count = new AtomicInteger(0);

    private final Lock lock = new ReentrantLock();
    private final Condition first = lock.newCondition();
    private final Condition second = lock.newCondition();
    private final Condition third = lock.newCondition();

    private volatile int state;

    public void first() throws InterruptedException {
        lock.lock();
        try {
            for (int i = 0; i < 5; i++) {
                while (state != 0) {
                    first.await();
                }
                for (int j = 0; j < 5; j++) {
                    System.out.println(
                            Thread.currentThread().getName() + "-" + count.incrementAndGet());
                }
                state = 1;
                second.signal();
            }
        } finally {
            lock.unlock();
        }
    }

    public void second() throws InterruptedException {
        lock.lock();
        try {
            for (int i = 0; i < 5; i++) {
                while (state != 1) {
                    second.await();
                }
                for (int j = 0; j < 5; j++) {
                    System.out.println(
                            Thread.currentThread().getName() + "-" + count.incrementAndGet());
                }
                state = 2;
                third.signal();
            }
        } finally {
            lock.unlock();
        }
    }

    public void third() throws InterruptedException {
        lock.lock();
        try {
            for (int i = 0; i < 5; i++) {
                while (state != 2) {
                    third.await();
                }
                for (int j = 0; j < 5; j++) {
                    System.out.println(
                            Thread.currentThread().getName() + "-" + count.incrementAndGet());
                }
                state = 0;
                first.signal();
            }
        } finally {
            lock.unlock();
        }
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5682886.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存