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

embedded/2025/2/26 17:26:01/

文章目录

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

前言

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

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

java">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;}@Overridepublic 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交替输出奇数和偶数

java">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

java">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;@Overridepublic void run() {while (cnt < 100) {cnt++;synchronized (T.class) {val++;}System.out.println(Thread.currentThread() + " " + cnt + " " + val);}}
}

模拟死锁

java">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;}@Overridepublic 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方法

java">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

java">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.ppmy.cn/embedded/167312.html

相关文章

排序算法模板——归并,快排【C++】

前言 二者都是分治思想的体现&#xff0c;区别是归并是以整个数组的mid&#xff08;下标的中间值&#xff09;来分&#xff0c;分别将左右两个区间排好序&#xff0c;再合并&#xff1b;而快排是以数组中的一个数来划分&#xff0c;将小于等于这个数的放在该数左边&#xff0c…

重构清洁想象,石头科技首创五轴仿生机械手打破传统清洁边界

2月25日&#xff0c;主题为“重构清洁想象”的石头科技2025发布会在上海天文馆正式召开。石头科技清洁产品BU总裁钱启杰在会上宣布&#xff0c;石头科技正式成为上海天文馆授权合作伙伴&#xff0c;希望借助航天科技到家庭科技的跨越&#xff0c;进一步简化家庭清洁工作&#x…

电商评论数据实现每秒百级评论数据的实时抓取

电商评论数据蕴含用户情感与产品改进方向。本文基于Go语言NSQ消息队列&#xff0c;实现每秒万级评论数据的实时抓取与情感分析。 1. ​系统架构与核心代码​ go package mainimport ("github.com/nsqio/go-nsq""encoding/json" )// 评论数据模型 type Com…

minio作为K8S后端存储

docker部署minio mkdir -p /minio/datadocker run -d \-p 9000:9000 \-p 9001:9001 \--name minio \-v /minio/data:/data \-e "MINIO_ROOT_USERjbk" \-e "MINIO_ROOT_PASSWORDjbjbjb123" \quay.io/minio/minio server /data --console-address ":90…

LLaMA中的微调方法

LoRA&#xff08;Low-Rank Adaptation&#xff09;是一种用于微调大型预训练模型的高效方法&#xff0c;特别适用于自然语言处理&#xff08;NLP&#xff09;任务。其核心思想是通过低秩分解来减少参数量&#xff0c;从而在保持模型性能的同时降低计算和存储成本。 关键点 低秩…

Starlink卫星动力学系统仿真建模第十讲-基于SMC和四元数的卫星姿态控制示例及Python实现

基于四元数与滑模控制的卫星姿态控制 一、基本原理 1. 四元数姿态表示 四元数运动学方程&#xff1a; 3. 滑模控制设计 二、代码实现&#xff08;Python&#xff09; 1. 四元数运算工具 import numpy as npdef quat_mult(q1, q2):"""四元数乘法""…

leetcode 2502. 设计内存分配器 中等

给你一个整数 n &#xff0c;表示下标从 0 开始的内存数组的大小。所有内存单元开始都是空闲的。 请你设计一个具备以下功能的内存分配器&#xff1a; 分配 一块大小为 size 的连续空闲内存单元并赋 id mID 。释放 给定 id mID 对应的所有内存单元。 注意&#xff1a; 多个…

10. docker nginx官方镜像使用方法

本文介绍docker nginx官方镜像使用方法&#xff0c;因为第一次用&#xff0c;在加上对docker也不是很熟&#xff0c;中间踩了一些坑&#xff0c;为了避免下一次用又踩坑&#xff0c;因此记录如下&#xff0c;也希望能够帮到其它小伙伴。 官方镜像页面&#xff1a;https://hub.d…