当前位置: 首页 > news >正文

【面试手撕】多线程/并发编程

文章目录

  • 前言
  • 三个线程,交替打印A、B、C
  • 两个线程1~100交替输出奇数和偶数
  • 10个线程,每个线程+1w,最终变量到达10w
  • 模拟死锁
  • 让三个线程怎么串行执行
    • 1.使用join方法
    • 2.使用CountDownLatch

前言

本文总结面试中常考的手撕多线程问题。

三个线程,交替打印A、B、C

package com.fwedu.question_;

/**
 * 三个线程交替打印A\B\C
 */
public class Question3 {
    private static final Object LOCK = new Object();
    private static volatile int count = 0;
    private static final int MAX = 101;

    public static void main(String[] args) {
        new Thread(new Seq(0)).start();
        new Thread(new Seq(1)).start();
        new Thread(new Seq(2)).start();
    }

    static class Seq implements Runnable {
        private final int index;

        Seq(int index) {
            this.index = index;
        }

        @Override
        public void run() {
            while (count < MAX) {
                synchronized (LOCK) {
                    try {
                        while (count % 3 != index) {
                            LOCK.wait();
                        }
                        if (count <= MAX) {
                            System.out.println("Thread-" + index + ":" + (char)('A' + count % 3));
                        }
                        count++;
                        LOCK.notifyAll();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

两个线程1~100交替输出奇数和偶数

package com.fwedu.question_;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 两个线程1~100交替输出奇数和偶数
 */
public class Question2 {
    private static CountDownLatch cl = new CountDownLatch(2);
    private static final Lock lock = new ReentrantLock();
    private static int cnt = 1;
    private static final int maxCnt = 100;

    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 2, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1));
        threadPoolExecutor.execute(() -> {
            while (cnt <= maxCnt) {
                lock.lock();
                try {
                    if (cnt <= maxCnt && cnt % 2 == 0) {
                        System.out.println(Thread.currentThread() + " " + cnt);
                        cnt++;
                    }
                } finally {
                    lock.unlock();
                }
            }
            cl.countDown();
        });
        threadPoolExecutor.execute(() -> {
            while (cnt <= maxCnt) {
                lock.lock();
                try {
                    if (cnt <= maxCnt && cnt % 2 != 0) {
                        System.out.println(Thread.currentThread() + " " + cnt);
                        cnt++;
                    }
                } finally {
                    lock.unlock();
                }
            }
            cl.countDown();
        });
        cl.await();
        threadPoolExecutor.shutdown();
    }
}

10个线程,每个线程+1w,最终变量到达10w

package com.fwedu.question_;

/**
 * 10个线程,每个线程+1w,最终变量到达10w
 */
public class Question1 {
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; ++i) {
            new Thread(new T()).start();
        }
        Thread.sleep(100);  // 确保上面的代码都执行完了
        System.out.println(T.val);
    }
}

class T extends Thread {
    static long val = 0;
    long cnt = 0;

    @Override
    public void run() {
        while (cnt < 100) {
            cnt++;
            synchronized (T.class) {
                val++;
            }
            System.out.println(Thread.currentThread() + " " + cnt + " " + val);
        }
    }
}

模拟死锁

package com.fwedu.syn;

/**
 * @author 冯威
 */
public class DeadLock {
    public static void main(String[] args) {
        //模拟死锁现象
        DeadLockDemo A = new DeadLockDemo(true);
        A.setName("A 线程");
        DeadLockDemo B = new DeadLockDemo(false);
        B.setName("B 线程");
        A.start();
        B.start();
    }
}

class DeadLockDemo extends Thread {
    static Object o1 = new Object();
    static Object o2 = new Object();
    boolean flag;

    public DeadLockDemo(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        if (flag) {
            synchronized (o1) {
                System.out.println(Thread.currentThread().getName() + " 进入 1");
                synchronized (o2) {
                    System.out.println(Thread.currentThread().getName() + " 进入 2");
                }
            }
        } else {
            synchronized (o2) {
                System.out.println(Thread.currentThread().getName() + " 进入 3");
                synchronized (o1) {
                    System.out.println(Thread.currentThread().getName() + " 进入 4");
                }
            }
        }
    }
}

让三个线程怎么串行执行

1.使用join方法

public class SerialExecutionUsingJoin {
    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(() -> {
            System.out.println("线程 1 执行");
        });

        Thread thread2 = new Thread(() -> {
            try {
                thread1.join();
                System.out.println("线程 2 执行");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread thread3 = new Thread(() -> {
            try {
                thread2.join();
                System.out.println("线程 3 执行");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        thread1.start();
        thread2.start();
        thread3.start();
    }
}

2.使用CountDownLatch

import java.util.concurrent.CountDownLatch;

public class SerialExecutionUsingCountDownLatch {
    public static void main(String[] args) {
        CountDownLatch latch1 = new CountDownLatch(1);
        CountDownLatch latch2 = new CountDownLatch(1);

        Thread thread1 = new Thread(() -> {
            System.out.println("线程 1 执行");
            latch1.countDown();
        });

        Thread thread2 = new Thread(() -> {
            try {
                latch1.await();
                System.out.println("线程 2 执行");
                latch2.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread thread3 = new Thread(() -> {
            try {
                latch2.await();
                System.out.println("线程 3 执行");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        thread1.start();
        thread2.start();
        thread3.start();
    }
}

相关文章:

  • 在 compare-form.vue 中添加 compareDate 隐藏字段,并在提交时自动填入当前时间
  • [OS] 基于RR(Round Robin)算法的CPU调度
  • ubuntu20.04 突破文件数限制
  • 前言:什么是大模型微调
  • 企业级大模型应用的Java-Python异构融合架构实践
  • 本地部署大模型: LM Studio、Open WebUI 与 Chatbox 全面对比以及选型指南
  • 在windows下安装windows+Ubuntu16.04双系统(上)
  • 公共数据授权运营模式研究(总体框架、主要模式及发展趋势)
  • linux下软件安装、查找、卸载
  • QT入门--QMainWindow
  • 2025年2月,TVBOX接口最新汇总版
  • C#实现本地AI聊天功能(Deepseek R1及其他模型)。
  • 五、 Spring Framework基础:Spring Data JPA基本用法与 Repository 接口
  • 在 Windows 上配置 Ollama 服务并开放局域网访问
  • Spring MVC框架二:创建第一个MVC程序
  • Redis-列表结构实操
  • labview实现有符号位16进制转二进制补码转真值
  • 数据结构与算法面试专题——桶排序
  • 前端浏览器开发中的浏览器兼容问题【持续更新】
  • 计算机毕业设计 ——jspssm502Springboot 餐厅点餐系统
  • “大型翻车现场”科技满满,黄骅打造现代化港口和沿海新城典范
  • 奥古斯都时代的历史学家李维
  • 女孩患异食癖爱吃头发,一年后腹痛入院体内惊现“头发巨石”
  • 占地57亩的“潮汕豪宅”面临强制拆除:曾被实施没收,8年间举行5次听证会
  • 三亚通报救护车省外拉警报器开道旅游:违规违法,责令公司停业整顿
  • 一海南救护车在西藏无任务拉警笛开道,墨脱警方:已处罚教育