2024年最新Java多线程循环打印ABC等问题(三种方式),java面试常问知识点

技术学习总结

学习技术一定要制定一个明确的学习路线,这样才能高效的学习,不必要做无效功,既浪费时间又得不到什么效率,大家不妨按照我这份路线来学习。

最后面试分享

大家不妨直接在牛客和力扣上多刷题,同时,我也拿了一些面试题跟大家分享,也是从一些大佬那里获得的,大家不妨多刷刷题,为金九银十冲一波!

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

这里一定要注意有一个坑,如果线程池核心线程数少于3个,1个或者2个,上述代码会有问题,因为线程池的队列是无界队列,多余核心线程数的任务会被放到队列中,这样循环打印的时候,有一些线程不是在运行中的,是在队列里的,运行中的线程会被阻塞,导致相互等待,死锁问题出现。所以线程池的核心线程数必须等于大于3个,这样保证有3个线程一直在运行。

2、volatile静态变量控制

还有一种就是将某个变量设置为静态,这样线程本身也共享该变量,则可以对其操作。但要注意给这个静态变量加上volatile字段,这个字段可以帮助多线程中的可见性。代码如下:

import java.util.concurrent.LinkedBlockingQueue;

import java.util.concurrent.ThreadPoolExecutor;

import java.util.concurrent.TimeUnit;

/**

  • 三个线程按次序轮流打印a,b,c

*/

public class StaticVarThread implements Runnable {

private static volatile Integer currentCount = 0;

private static final Integer MAX_COUNT = 6;

private static String[] chars = {“a”, “b”, “c”};

private String name;

public StaticVarThread(String name) {

this.name = name;

}

@Override

public void run() {

while (currentCount < MAX_COUNT) {

try {

while (this.name.equals(chars[currentCount % 3]) && currentCount < MAX_COUNT) {

printAndPlusOne(this.name);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

}

}

}

public void printAndPlusOne(String name) {

System.out.println(name + “\t” + currentCount);

currentCount++;

}

public static void main(String[] args) {

ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 3, 20, TimeUnit.MINUTES, new LinkedBlockingQueue());

threadPoolExecutor.execute(new StaticVarThread(“a”));

threadPoolExecutor.execute(new StaticVarThread(“b”));

threadPoolExecutor.execute(new StaticVarThread(“c”));

threadPoolExecutor.shutdown();

}

}

3、synchronized对象加锁

利用一个计数来除线程个数得到余数从而控制线程输出,并且利用synchronized、一个Object的wait()和notify()方法来对线程的阻塞和唤醒,这里的技巧可以看看。

  • wait()

阻塞当前线程,等待被释放,继续执行当前线程

  • notify()

唤醒监视该对象的第一个线程,notifyAll()唤醒监视该对象的所有线程。

import org.junit.Test;

import java.util.concurrent.LinkedBlockingQueue;

import java.util.concurrent.ThreadPoolExecutor;

import java.util.concurrent.TimeUnit;

public class SynchronizedThread {

static volatile int state; // 线程共有,判断所有的打印状态

static final Object t = new Object(); // 线程共有,加锁对象

/**

  • 改装了一下

*/

@Test

public void mainSix() {

state = 0;

// 需要循环的次数

int n = 10;

int num = 2;

ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 2, 20, TimeUnit.MINUTES, new LinkedBlockingQueue<>());

threadPoolExecutor.execute(new MyThread(“A”, 0, n, num));

threadPoolExecutor.execute(new MyThread(“B”, 1, n, num));

threadPoolExecutor.shutdown();

}

class MyThread implements Runnable {

String name;

int which; // 标示符:0:打印A;1:打印B;2:打印C

int n; // 0:打印次数

int num; // 循环周期

public MyThread(String name,int which,int n,int num) {

this.name = name;

this.which = which;

this.n = n;

this.num = num;

}

@Override

最后

文章中涉及到的知识点我都已经整理成了资料,录制了视频供大家下载学习,诚意满满,希望可以帮助在这个行业发展的朋友,在论坛博客等地方少花些时间找资料,把有限的时间,真正花在学习上,所以我把这些资料,分享出来。相信对于已经工作和遇到技术瓶颈的朋友们,在这份资料中一定都有你需要的内容。

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

出来。相信对于已经工作和遇到技术瓶颈的朋友们,在这份资料中一定都有你需要的内容。**

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  • 20
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值