A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© blackstones1 中级黑马   /  2014-6-14 18:57  /  1032 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

        new Thread(new Runnable()
                {
                        public void run()
                        {
                                System.out.println("1runnable run....");
                        }
                })
                {
                        public void run()
                        {
                                System.out.println("2subThread...run....");
                        }
                }.start();
这段代码行不行  要怎么理解啊 是不是算2个线程呢  新手求大婶详解


3 个回复

倒序浏览
只算一个,
在线程Thread类的源文件中,run()方法是这样定义的:
  1.     public void run() {
  2.         if (target != null) {
  3.             target.run();
  4.         }
  5.     }
复制代码

变量target就是构造函数中的实现了Runnable接口的变量,如果你没有重写run的方法,那他就会调用target的run()方法,你重写了方法后,就会用你重写的,传进去的参数也就没啥作用了!
回复 使用道具 举报
而且你的标点符号也多打了    “    })   ”
回复 使用道具 举报
本帖最后由 西门吹风 于 2014-6-14 21:43 编辑

应该是一个线程,并且运行的是Thread子类中run 的内容,可以分解成如下代码来解理
  1. class  ThreadTest
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 A a=new A();
  6.                 B b=new B(a);         //相当于new Runnable(new Runnable{}){}
  7.                 b.start();               
  8.         }
  9. }
  10. class A implements Runnable       //相当于new Runnable(){}
  11. {
  12.         public void run()
  13.         {
  14.                 System.out.println("1runnable run...");
  15.         }
  16. }
  17. class B extends Thread       //相当于 new Thread(){}
  18. {
  19.         B(Runnable b)
  20.         {
  21.                 super(b);
  22.         }
  23.         public void run()
  24.         {
  25.                 System.out.println("2subThread...run...");
  26.         }
  27. }
复制代码


回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马