JAVA多线程编程,创建3个线程分别打印A,B和C,打印10次
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA多线程编程,创建3个线程分别打印A,B和C,打印10次相关的知识,希望对你有一定的参考价值。
例如打印效果ABCABCABC.....10次,是有次序的ABC.不能乱哦
效果如下:
线程1打印A
线程2打印B
线程3打印C
线程1打印A
线程2打印B
线程3打印C
线程1打印A
线程2打印B
线程3打印C
..............
这种持续打印10次的格式
private static volatile String msg = "线程一打印A";
private String message;
private String next;
public Test(String message, String next)
this.message = message;
this.next = next;
public static void main(String[] args)
new Thread(new Test("线程一打印A", "线程二打印B")).start();
new Thread(new Test("线程二打印B", "线程三打印C")).start();
new Thread(new Test("线程三打印C", "线程一打印A")).start();
public void run()
for(int i = 0; i < 10;)
if(this.message.equals(Test.msg))
System.out.println(this.message);
Test.msg = next;
i++;
这样写不需要同步代码, 应该会更自然点
public static void main(String[] args)
Threands t = new Threands();
new Thread(t).start();//线程1
new Thread(t).start();//线程2
new Thread(t).start();//线程3
class Threands implements Runnable
private String[] arr = "ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC";
private int i = 0;
public synchronized void run()
for( ; i < arr.length; i++)
System.out.print(arr[i]);
//或者更简单
class Threands implements Runnable
//private String[] arr = "ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC";
private String str = "ABC";
private int i = 0;
public synchronized void run()
for( ; i < 10; i++)
System.out.print(str);
追问
可能是我没说明白.分别打印A,B和C.
比如效果:
线程1打印A
线程2打印B
线程3打印C
线程1打印A
线程2打印B
线程3打印C
线程1打印A
线程2打印B
线程3打印C
..............
这种持续打印10次的格式
小菜一碟!
追问好了回答我一下哈.
追答源码写好了,但是发不了,代码太多,你留个邮箱好不好!
追问能有多少啊.也就百来行吧.直接发来看看.下面那哥们发的都有100行啊,谢了
追答我这是追答,字数有限制!有采纳答案就算了,祝你编程愉快!
参考技术B public class TestThreadprivate static String status = "A";
public static void main(String[] args)
new Thread(new Runnable()
@Override
public void run()
int count=10;
for(int i=0;i<count;)
synchronized (status)
if(status.equals("A"))
System.out.print("A");
status="B";
i++;
).start();
new Thread(new Runnable()
@Override
public void run()
int count=10;
for(int i=0;i<count;)
synchronized (status)
if(status.equals("B"))
System.out.print("B");
status="C";
i++;
).start();
new Thread(new Runnable()
@Override
public void run()
int count=10;
for(int i=0;i<count;)
synchronized (status)
if(status.equals("C"))
System.out.print("C");
status="A";
i++;
).start();
追问
我试了,效果是没问题.
这个在实际应用当中用这种方式是否是比较准确,还有更优化的方式么.就是完全用线程来弄,不用判断A或者B或者C
这个明显不可能,你要多线程运行,还要他们按规定的顺序,你不做同步不可能的呀
举个例子,线程B必须在线程A打印后立刻执行,而且要保证不让线程C抢先执行,所以必须要做一定的监控,不可能放着所有的线程自由执行
记一个Java多线程相关的面试题
最近一个朋友在找工作,今天给我分享了一个面试题:创建n个线程,循环打印m次(a-z,A-Z,大小写间隔输出) 例如: 3个线程 打印2次(a-z,A-Z) 输出结果 thread1-a,thread2-A,thread3-b。。。。循环两次。大概意思就是创建指定数量n的线程循环打印m次,循环内容是线程一次执行,分别从两个共享资源交错取数据。好像越说越乱的样子emmm。。。
最终要的结果如下:
楼主一听题,这还不简单,考的就是多线程的嘛,结果撸了俩小时。。。先贴代码吧。
package com.XXX.manage.modules; import java.util.concurrent.atomic.AtomicInteger; /** * @author JohanChan * @Description TODO * @time 2021/6/3 15:14 */ public class Demo { //需要打印的常量 private static final String[] XXZM = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}; private static final String[] DXZM = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}; private static volatile boolean isXXZM = true;//区分执行数组 private static volatile AtomicInteger count = new AtomicInteger(1);//判断当前要执行的线程 private static volatile AtomicInteger index = new AtomicInteger(0);//打印字母的下标 private static volatile AtomicInteger forCount = new AtomicInteger(0);//循环次数 /** * 功能描述: * TODO * * @param n 线程数量 * @param m 循环次数 * @return void * @author JohanChan * @date 2021/6/3 17:36 */ public static void demo(int n,int m){ if (n > 0 && m > 0){ //每次执行前重置共享资源 isXXZM = true;count.set(1);index.set(0);forCount.set(0); for (int i = 0;i < n;i++){ int thisNUm = i + 1; Thread thread = new Thread(new Runnable() { private final int num = thisNUm;//本线程执行的顺序标识 @Override public void run() { System.out.println("开始执行线程-" + num); //循环次数小于m,打印 while (forCount.get() < m) { print(n, num); } } }); thread.setName("Thread" + thisNUm); thread.start(); } } } /** * 功能描述: * TODO * * @param n 需要执行的线程顺序 * @param num 当前线程顺序标识 * @return void * @author JohanChan * @date 2021/6/3 17:37 */ private static void print(int n,int num){ //获取当前线程 Thread thread = Thread.currentThread(); //判断执行顺序 if (count.get() == num){ synchronized (XXZM){ if (isXXZM){//小写字母 System.out.println(thread.getName() + "-" + XXZM[index.get()]); isXXZM = false; }else{//大写字母 System.out.println(thread.getName() + "-" + DXZM[index.get()]); isXXZM = true; //打印Z后重置下标,循环次数加1 if (index.get() == DXZM.length - 1){ index.set(0);//下标重置为0 forCount.incrementAndGet();//循环次数加1 }else{ index.incrementAndGet();//下标加1 } } //执行线程的顺序标记重置或加1 if (count.get() == n){ count.set(1); }else{ count.incrementAndGet(); } XXZM.notifyAll(); } }else{ try { XXZM.wait(); }catch (Exception e){} } } public static void main(String[] args){ demo(5,10); } }
不得不说,代码要经常撸,这个题没有实际的业务场景支撑,考察的就是多线程相关的应用,楼主有点眼高手低了,以后整理下相关的知识点巩固一下。竟然浪费的两个小时的时间,不说了,下班了,撤!
以上是关于JAVA多线程编程,创建3个线程分别打印A,B和C,打印10次的主要内容,如果未能解决你的问题,请参考以下文章