Thread actor=new Thread(new Actor(),"Mr.Thread");

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

慕神1905738

2016-10-25 17:07

为什么如题的代码运行结果不对 编译器也不报错呢

写回答 关注

3回答

  • 慕神1905738
    2016-10-25 18:11:57

    package com.imooc.concurrent;


    public class Actor extends Thread {


    public void run() {

    System.out.println(getName() + "is an actor");

    int count = 0;

    boolean a=true;

    while (a){

    System.out.println(getName() + "show begins" + (++count));

    if (count==100)

    {

    a=false;

    }

    if(count%10==0){

    try {

    Thread.sleep(1000);

    } catch (InterruptedException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    }

    }

    // System.out.println(getName() + "show begins" + (++count));

    System.out.println(getName() + "show is over");

    }


    public static void main(String[] args) {

    // Thread actor = new Actor();

    //

    // actor.setName("Mr.Thread");

    Thread actor=new Thread(new Actor(),"Mr.Thread");

    actor.start();

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

    actressThread.start();

    }


    }


     class Actress implements Runnable{

    public void run() {

    System.out.println(Thread.currentThread().getName() + "is an actor");

    int count = 0;

    boolean a=true;

    while (a){

    System.out.println(Thread.currentThread().getName() + "show begins" + (++count));

    if (count==100)

    {

    a=false;

    }

    if(count%10==0){

    try {

    Thread.sleep(1000);

    } catch (InterruptedException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    }

    }

    // System.out.println(getName() + "show begins" + (++count));

    System.out.println(Thread.currentThread().getName() + "show is over");

    }

     }


  • 1039732307
    2016-10-25 17:51:59

    Actor是否实现了Runable或继承了Thread,run方法是否有问题

    慕神1905...

    代码加进来了 请看看是哪里的问题 谢谢

    2016-10-25 18:16:05

    共 1 条回复 >

  • 慕慕7454464
    2016-10-25 17:41:27

    查看下 Actor类 是否实现了 runable接口

    慕神1905...

    代码加进来了 请帮忙看看是哪里的问题 谢谢

    2016-10-25 18:15:29

    共 1 条回复 >

深入浅出Java多线程

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

186088 学习 · 464 问题

查看课程

相似问题