• Java多线程按顺序输出10以内的奇偶数


    创建两个线程,一个线程输出奇数,一个线程输出偶数,实现按照1~10的顺序输出

    代码实现1

    public class OddEvenNumber {
        // volatile关键字修饰的变量保证了可见性,即对该变量的写操作对其他线程可见。
        private volatile int num = 1;
        public static void main(String[] args) {
            OddEvenNumber number = new OddEvenNumber();
            new Thread(() -> number.technology()).start();
            new Thread(() -> number.even()).start();
        }
    
        public synchronized void technology() {
            while (num < 11) {
                if (num % 2 != 0) {
                    System.out.println(num);
                    num++;
                } else {
                    notify();
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    
        public synchronized void even() {
            while (num < 11) {
                if (num % 2 == 0) {
                    System.out.println(num);
                    num++;
                } else {
                    notify();
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    代码实现2

    import java.util.concurrent.*;
    import java.util.concurrent.atomic.AtomicInteger;
    
    public class ThreadPoolExecutorExample {
        public static void main(String[] args) {
            // 核心线程数为2,最大线程数为2,线程空闲时间为10秒,使用无边界队列
            // 使用默认的线程工厂和饱和策略
            ThreadPoolExecutor executor = new ThreadPoolExecutor(
                    2, 2, 1, TimeUnit.SECONDS, new LinkedBlockingDeque<>() //, new ThreadPoolExecutor.CallerRunsPolicy()
            );
    
            // 提交任务给线程池执行
            // submit()方法也接受一个 Runnable 对象作为参数,但它返回一个 Future 对象,用于异步获取任务执行结果或者取消任务执行。
            // 方法没有返回值,如果任务执行过程中出现异常或错误,它不会抛出该异常或错误,只会简单地打印出异常栈信息。
    
            AtomicInteger num = new AtomicInteger(1);  // 初始值为1,线程安全的,原子类
    
            executor.submit(() -> {
                while (num.get() <= 10) {
                    if (num.get() % 2 == 0) {
                        System.out.println(num.get());
                        num.incrementAndGet();  // 等同于 num++
                    }
                }
            });
            // execute()方法接受一个 Runnable 对象作为参数,将其提交到线程池中执行。
            // 方法没有返回值,如果任务执行过程中出现异常或错误,它不会抛出该异常或错误,只会简单地打印出异常栈信息。
            executor.execute(() -> {
                while (num.get() < 10) {
                    if (num.get() % 2 != 0) {
                        System.out.println(num.get());
                        num.incrementAndGet();  // 等同于 num++
                    }
                }
            });
    
            //关闭线程池
            executor.shutdown();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
  • 相关阅读:
    短视频优质作者必备|配音神器分享|那些你刷视频时肯定听过的声音
    系统架构设计:9 论软件系统架构评估及其应用
    Jenkins+Pipeline Script+Groovy+Mysql 持续集成配置
    【LeetCode每日一题】——225.用队列实现栈
    光遇三周年福利【源码+教程】Java游戏开发_Java贪吃蛇小游戏_Java项目实战_Java课程设计_Java课设项目_Java初级项目_Java练手项目
    Request Body Search
    免费使用ChatGPT 4.0 和 文心一言 4.0
    日常SRC中xss小tips
    吴恩达2022机器学习_第二部分高级学习算法笔记
    论坛回顾|用社区和开发者工具驱动软件供应链安全治理——章华鹏
  • 原文地址:https://blog.csdn.net/bao_14440/article/details/133045671