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

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

文章目录

  • 前言
  • 三个线程,交替打印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();
    }
}

http://www.kler.cn/a/564739.html

相关文章:

  • OpenCV(11):人脸检测、物体识别
  • 新生校园报道小程序毕业系统设计
  • 【Java项目】基于Spring Boot的校园博客系统
  • 使用 Postman 访问 Keycloak 端点
  • .gitignore 文件中添加忽略 .pdb 文件
  • Readability.js 与 Newspaper提取网页内容和元数据
  • Linux LCD驱动开发
  • 【后端开发面试题】每日 3 题(一)
  • 12个大语言模型平台对比测试-搜索视角
  • 设计模式教程:模板方法模式(Template Method Pattern)
  • 流式抽样唯一元素方案设计
  • 快速上手 Unstructured:安装、Docker部署及PDF文档解析示例
  • DeepSeek 助力 Vue3 开发:打造丝滑的页眉(Header)
  • 川翔云电脑优势总结
  • MyBatis TypeHandler 详解与实战:FastJson 实现字符串转 List
  • Java从根上理解 ConcurrentHashMap:缓存机制与性能优化
  • 矩阵碰一碰发视频源码搭建之,支持OEM
  • RFID测温技术:为生产安全与稳定保驾护航
  • wayland桌面录屏,屏幕录制,Linux屏幕录制
  • leetcode 3008. 找出数组中的美丽下标 II