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

Java每日一练02数组元素统计,多线程指定执行顺序

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

Java每日一练02数组元素统计,多线程指定执行顺序

Java每日一练02
  • 数组元素统计
  • 多线程按指定顺序执行
    • 题目描述
    • Semaphore信号量实现
    • synchronized同步锁实现
    • volatile实现
    • CountDownLatch倒计时门闩实现

数组元素统计
class RandomTest {
	
    public static void main(String[] args) {
        // 输出arr1数组
        int[] arr1 = storingRandomNumbers(5, 1, 9);
        System.out.println(Arrays.toString(arr1));
        int[] ints = numberModuloStatistics(arr1, 1, 3);
        System.out.println(Arrays.toString(ints));
    }

    
    private static int[] storingRandomNumbers(int quantity, int start, int end) {
        int arr1[] = new int[quantity];
        for (int i = start - 1; i < arr1.length; i++) {
            arr1[i] = new Random().nextInt(end) + 1;
        }
        return arr1;
    }

    
    private static int[] numberModuloStatistics(int[] arr1,int modnum1,int modnum2) {
        int i2 = 0,j3 = 0;
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] % modnum1 == 0) {
                i2++;
            }
            if (arr1[i] % modnum2 == 0) {
                j3++;
            }
        }
        int arr2[] = new int[2];
        arr2[0] = i2;
        arr2[1] = j3;
        return arr2;
    }
}
多线程按指定顺序执行 题目描述

import java.util.concurrent.Semaphore;

class Demo {
    public static void main(String[] args) {
        AnotherDummy ad = new AnotherDummy();

        Thread t1 = new Thread(() -> {
            try {
                ad.first(() -> {
                    // business logic code
                    System.out.println("first");
                });
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread t2 = new Thread(() -> {
            try {
                ad.second(() -> {
                    // business logic code
                    System.out.println("second");
                });
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread t3 = new Thread(() -> {
            try {
                ad.third(() -> {
                    // business logic code
                    System.out.println("third");
                });
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        
        t2.start();
        t1.start();
        t3.start();
    }
}
Semaphore信号量实现
class AnotherDummy {
    // The count of permits is 0, the threads will be blocked.
    // Until, the resource can be acquired.
    private Semaphore first = new Semaphore(0);
    private Semaphore second = new Semaphore(0);
    private Semaphore third = new Semaphore(0);

    public void first(Runnable runnable) throws InterruptedException {
        runnable.run();
        first.release(); // release方法会释放持有许可证的线程,并归还Semaphore一个可用的许可证
        second.release();
    }

    public void second(Runnable runnable) throws InterruptedException {
        second.acquire(); // 调用acquire方法会阻塞线程执行,直到获取下一个许可证
        runnable.run();
        second.release();
        third.release();
    }

    public void third(Runnable runnable) throws InterruptedException {
        third.acquire();
        runnable.run();
        third.release();
    }
}
synchronized同步锁实现
class Foo {
    private boolean firstFinished;
    private boolean secondFinished;
    private Object lock = new Object();

    public Foo() {

    }

    public void first(Runnable printFirst) throws InterruptedException {
        synchronized (lock) {
            printFirst.run();
            firstFinished = true;
            lock.notifyAll(); // 唤醒其它线程
        }
    }

    public void second(Runnable printSecond) throws InterruptedException {
        synchronized (lock) {
            while (!firstFinished) {
                lock.wait();
            }

            printSecond.run();
            secondFinished = true;
            lock.notifyAll(); // 唤醒其它线程
        }
    }

    public void third(Runnable printThird) throws InterruptedException {
        synchronized (lock) {
            while (!secondFinished) {
                lock.wait();
            }
            printThird.run();
        }
    }
}
volatile实现
class Foo2 {
    public Foo2() {

    }
    volatile int count=1;
    public void first(Runnable printFirst) throws InterruptedException {
        printFirst.run();
        count++;
    }

    public void second(Runnable printSecond) throws InterruptedException {
        while (count!=2);
        printSecond.run();
        count++;
    }

    public void third(Runnable printThird) throws InterruptedException {
        while (count!=3);
        printThird.run();
    }
}
CountDownLatch倒计时门闩实现
class Foo3 {
    
    private CountDownLatch second = new CountDownLatch(1);
    private CountDownLatch third = new CountDownLatch(1);
    public Foo3() {

    }

    public void first(Runnable printFirst) throws InterruptedException {
        printFirst.run();
        second.countDown();
    }

    public void second(Runnable printSecond) throws InterruptedException {
        second.await();
        printSecond.run();
        third.countDown();
    }

    public void third(Runnable printThird) throws InterruptedException {
        third.await();
        printThird.run();
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/686223.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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