为什么Runnable没有运行结果

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

奋斗的小牧童

2015-04-28 14:52

package com.imooc.concurrent;

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 actressThread = new Thread();
        actressThread.setName("Ms.Runnable");
        actressThread.start();
    }

class Actress implements Runnable{

        @Override
        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()+"演出结束了!");
        }
        
    }



}

结果:

Mr.Thread演出开始了!
Mr.Thread登台演出:1
Mr.Thread登台演出:2
Mr.Thread登台演出:3
Mr.Thread登台演出:4
Mr.Thread登台演出:5
Mr.Thread登台演出:6
Mr.Thread登台演出:7
Mr.Thread登台演出:8
Mr.Thread登台演出:9
Mr.Thread登台演出:10
Mr.Thread登台演出:11
Mr.Thread登台演出:12
Mr.Thread登台演出:13
Mr.Thread登台演出:14
Mr.Thread登台演出:15
Mr.Thread登台演出:16
Mr.Thread登台演出:17
Mr.Thread登台演出:18
Mr.Thread登台演出:19
Mr.Thread登台演出:20
Mr.Thread登台演出:21
Mr.Thread登台演出:22
Mr.Thread登台演出:23
Mr.Thread登台演出:24
Mr.Thread登台演出:25
Mr.Thread登台演出:26
Mr.Thread登台演出:27
Mr.Thread登台演出:28
Mr.Thread登台演出:29
Mr.Thread登台演出:30
Mr.Thread登台演出:31
Mr.Thread登台演出:32
Mr.Thread登台演出:33
Mr.Thread登台演出:34
Mr.Thread登台演出:35
Mr.Thread登台演出:36
Mr.Thread登台演出:37
Mr.Thread登台演出:38
Mr.Thread登台演出:39
Mr.Thread登台演出:40
Mr.Thread登台演出:41
Mr.Thread登台演出:42
Mr.Thread登台演出:43
Mr.Thread登台演出:44
Mr.Thread登台演出:45
Mr.Thread登台演出:46
Mr.Thread登台演出:47
Mr.Thread登台演出:48
Mr.Thread登台演出:49
Mr.Thread登台演出:50
Mr.Thread登台演出:51
Mr.Thread登台演出:52
Mr.Thread登台演出:53
Mr.Thread登台演出:54
Mr.Thread登台演出:55
Mr.Thread登台演出:56
Mr.Thread登台演出:57
Mr.Thread登台演出:58
Mr.Thread登台演出:59
Mr.Thread登台演出:60
Mr.Thread登台演出:61
Mr.Thread登台演出:62
Mr.Thread登台演出:63
Mr.Thread登台演出:64
Mr.Thread登台演出:65
Mr.Thread登台演出:66
Mr.Thread登台演出:67
Mr.Thread登台演出:68
Mr.Thread登台演出:69
Mr.Thread登台演出:70
Mr.Thread登台演出:71
Mr.Thread登台演出:72
Mr.Thread登台演出:73
Mr.Thread登台演出:74
Mr.Thread登台演出:75
Mr.Thread登台演出:76
Mr.Thread登台演出:77
Mr.Thread登台演出:78
Mr.Thread登台演出:79
Mr.Thread登台演出:80
Mr.Thread登台演出:81
Mr.Thread登台演出:82
Mr.Thread登台演出:83
Mr.Thread登台演出:84
Mr.Thread登台演出:85
Mr.Thread登台演出:86
Mr.Thread登台演出:87
Mr.Thread登台演出:88
Mr.Thread登台演出:89
Mr.Thread登台演出:90
Mr.Thread登台演出:91
Mr.Thread登台演出:92
Mr.Thread登台演出:93
Mr.Thread登台演出:94
Mr.Thread登台演出:95
Mr.Thread登台演出:96
Mr.Thread登台演出:97
Mr.Thread登台演出:98
Mr.Thread登台演出:99
Mr.Thread登台演出:100
Mr.Thread演出结束了!

写回答 关注

4回答

  • Arthur
    2015-04-28 22:55:52

    你还没理解如何创建线程。

    奋斗的小牧童

    怎么修改

    2015-04-29 14:24:47

    共 1 条回复 >

  • 收拾好鑫情
    2016-01-07 10:59:50

    28行那里要添加一个Actress的对象。要不然谁知道你执行哪个线程

  • SunshineCui
    2015-04-29 10:15:45

    第28行代码不对吧

  • 晕_代码
    2015-04-28 21:02:35

     Thread actressThread = new Thread(new Actress());改成这样,你运行Runnable方法错了

深入浅出Java多线程

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

186088 学习 · 464 问题

查看课程

相似问题