栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

多线程交替打印数字

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

多线程交替打印数字

 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();
        }
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/682312.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号