为什么运行结果与老师不同?

来源:2-3 Java线程-隋唐演义实战开发---演员简介

慕斯卡3197864

2018-04-21 20:25

代码与老师一样,但运行结果如下:

https://img3.mukewang.com/5adb2cd50001b86d02280248.jpg

并不是每个线程分别演出10次,这是什么原因呢?看了下别的答案好像和电脑的CPU核数有关,但还不是很清楚,有人能比较清楚地介绍下吗?

另外,怎么控制才能和老师的输出结果一样呢?

万分感谢!


以下是我的代码

public class Actor extends Thread {
    public void run() {
       System.out.println(getName()+"登台演出了!");
       int count=0;
       boolean keepRunning=true;
       while(keepRunning) {
           System.out.println(getName()+"登台演出了:"+(++count));
           if(count==100)
              keepRunning=false;
           if(count%10==0) {
              try {
                  Thread.sleep(1000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
           }
       }
       System.out.println(getName()+"的演出结束了");
    }
   
    public static void main(String[] args) {
       Thread actor= new Actor();     
       actor.setName("Mr. Thread");
       actor.start();
       Thread actress = new Thread(new Actress(), "Ms. Runnable");
       actress.start();
    }
}
 
class Actress implements Runnable{
    public void run() {
       System.out.println(Thread.currentThread().getName()+"登台演出了!");
       int count=0;
       boolean keepRunning=true;
       while(keepRunning) {
           System.out.println(Thread.currentThread().getName()+"登台演出了:"+(++count));
           if(count==100)
              keepRunning=false;
           if(count%10==0) {
              try {
                  Thread.sleep(1000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
           }
       }
       System.out.println(Thread.currentThread().getName()+"的演出结束了");
    }
}


写回答 关注

2回答

  • TendeRess
    2018-07-17 12:28:32

    嘿嘿,多运行几次你就会发现,结果不会总是一样的,多核计算机的时间片分配问题

  • 慕斯卡3197864
    2018-04-21 20:34:20
    public class Actor extends Thread {

        public void run() {

           System.out.println(getName()+"登台演出了!");

           int count=0;

           boolean keepRunning=true;

           while(keepRunning) {

               System.out.println(getName()+"登台演出了:"+(++count));

               if(count==100)

                  keepRunning=false;

               if(count%10==0) {

                  try {

                      Thread.sleep(1000);

                  } catch (InterruptedException e) {

                      e.printStackTrace();

                  }

               }

           }

           System.out.println(getName()+"的演出结束了");

        }

       

        public static void main(String[] args) {

           Thread actor= new Actor();     

           actor.setName("Mr. Thread");

           actor.start();

           Thread actress = new Thread(new Actress(), "Ms. Runnable");

           actress.start();

        }

    }

     

    class Actress implements Runnable{

        public void run() {

           System.out.println(Thread.currentThread().getName()+"登台演出了!");

           int count=0;

           boolean keepRunning=true;

           while(keepRunning) {

               System.out.println(Thread.currentThread().getName()+"登台演出了:"+(++count));

               if(count==100)

                  keepRunning=false;

               if(count%10==0) {

                  try {

                      Thread.sleep(1000);

                  } catch (InterruptedException e) {

                      e.printStackTrace();

                  }

               }

           }

           System.out.println(Thread.currentThread().getName()+"的演出结束了");

        }

    }


    RSF 回复慕斯卡319...

    那你知道怎么改成跟老师一样的吗??/

    2018-06-20 14:13:09

    共 3 条回复 >

深入浅出Java多线程

带你一起深入浅出多线程,掌握基础,展望进阶路线

186088 学习 · 464 问题

查看课程

相似问题