MaolinYe(叶茂林) 2023-11-26 21:20 采纳率: 61.9%
浏览 10
已结题

一个Java线程调度的问题,运行多次,三个线程打印字符 a b 和数字总是先输出完字母,再输出数字是为什么

一个Java线程调度的问题,运行多次,三个线程打印字符 a b 和数字总是先输出完字母,再输出数字是为什么


class PrintChar implements Runnable{
    private char charToPrint;
    private int times;
    public PrintChar(char c,int t){
        charToPrint=c;
        times=t;
    }

    @Override
    public void run() {
        for(int i=0;i<times;i++){
            System.out.print(charToPrint);
        }
    }
}
class PrintNum implements Runnable{
    private int lastNum;
    public PrintNum(int n){
        lastNum=n;
    }

    @Override
    public void run() {
        for(int i=0;i<=lastNum;i++){
            System.out.print(" "+i);
        }
    }
}
public class TaskThreadDemo {
    public static void main(String[] args) {
        Runnable printA=new PrintChar('a',100);
        Runnable printB=new PrintChar('b',100);
        Runnable print100=new PrintNum(100);
        Thread thread1=new Thread(printA);
        Thread thread2=new Thread(printB);
        Thread thread3=new Thread(print100);
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

  • 写回答

3条回答 默认 最新

查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 12月5日
  • 已采纳回答 11月27日
  • 创建了问题 11月26日

悬赏问题

  • ¥20 Html备忘录页面制作
  • ¥15 黄永刚的晶体塑性子程序中输入的材料参数里的晶体取向参数是什么形式的?
  • ¥20 数学建模来解决我这个问题
  • ¥15 计算机网络ip分片偏移量计算头部是-20还是-40呀
  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败