多线程交替输出问题3种常用解法(多线程经典面试题)
发布日期:2022-04-11 08:52:50 浏览次数:5 分类:技术文章

本文共 3849 字,大约阅读时间需要 12 分钟。

题目:让两个多线程交替输出

举例:“ABCDEF”和“123456”,
输出结果为固定“A1B2C3D4E5F6”

1.利用synchronized+wait/notify

package com.mooc.house.user.controller;/** * @ClassName sync_wait_notify * @Description DOTO * @Author mischen * @Date 2021/9/30 0030 6:11 * @Version 1.0 **/public class sync_wait_notify {
public static void main(String[] args) {
final Object o = new Object(); char[] aI = "ABCDEF".toCharArray(); char[] aC = "123456".toCharArray(); new Thread(()->{
synchronized (o) {
for(char c : aI) {
System.out.print(c); try {
o.notify(); o.wait(); //让出锁 } catch (InterruptedException e) {
e.printStackTrace(); } } o.notify(); //必须,否则无法停止程序 } }, "t1").start(); new Thread(()->{
synchronized (o) {
for(char c : aC) {
System.out.print(c); try {
o.notify(); o.wait(); } catch (InterruptedException e) {
e.printStackTrace(); } } o.notify(); } }, "t2").start(); }}

2.利用condition和ReentrantLock联合使用

package com.mooc.house.user.controller;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;/** * @ClassName T01_00_Question * @Description DOTO * @Author mischen * @Date 2021/9/30 0030 6:17 * @Version 1.0 **/public class T01_00_Question {
private char[] a1 = {
'1','2','3','4','5','6'}; private char[] a2 = {
'A','B','C','D','E','F'}; private ReentrantLock lock = new ReentrantLock(); private Condition c1 = lock.newCondition(); private Condition c2 = lock.newCondition(); public void m1(){
int count = 0; lock.lock(); //这里模拟前面四个数 while(count <= 5){
System.out.print(a2[count++]); try {
c2.signal(); c1.await(); } catch (InterruptedException e) {
e.printStackTrace(); } } c2.signal(); lock.unlock(); } public void m2(){
int count = 0; lock.lock(); while(count <= 5){
System.out.print(a1[count++]); try {
c1.signal(); c2.await(); } catch (InterruptedException e) {
e.printStackTrace(); } } c1.signal(); lock.unlock(); } public static void main(String[] args) {
//要求用线程顺序打印A1B2C3....Z26 T01_00_Question t = new T01_00_Question(); new Thread(()->{
t.m1(); }).start(); new Thread(()->{
t.m2(); }).start(); }}

3.利用LockSupport

package com.mooc.house.user.controller;import java.util.concurrent.locks.LockSupport;/** * @ClassName Test1 * @Description DOTO * @Author mischen * @Date 2021/9/30 0030 5:42 * @Version 1.0 **/public class Test1 {
static Thread t1 = null,t2=null; public static void main(String[] args) {
char[] a1= "ABCDEF".toCharArray(); char[] c1= "123456".toCharArray(); t1 = new Thread(() -> {
for(char c : a1) {
System.out.print(c); LockSupport.unpark(t2); //叫醒T2 LockSupport.park(); //T1阻塞 } }, "t1"); t2 = new Thread(() -> {
for(char c : c1) {
LockSupport.park(); //t2阻塞 System.out.print(c); LockSupport.unpark(t1); //叫醒t1 } }, "t2"); t1.start(); t2.start(); }}

转载地址:https://blog.csdn.net/miachen520/article/details/120559448 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:多线程介绍
下一篇:多线程交叉打印数字(1-75)

发表评论

最新留言

很好
[***.229.124.182]2024年04月20日 01时31分21秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章