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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 騛鹏 中级黑马   /  2013-3-16 17:28  /  1808 人查看  /  10 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class Test extends Thread
{
        private String name;
        Test(String name)
        {
                //super(name);
                this.name = name;
        }
        public void run()
        {
                for (int x=0; x<60; x++)
                {
                        System.out.println(this.getName()+"-----"+x);
                }
        }
}
class  ThreadTest
{
        public static void main(String[] args)
        {
                Test t1 = new Test("one-----");
                Test t2 = new Test("two+++++");
                t1.start();
                t2.start();

                for (int x=0; x<60; x++)
                {
                        System.out.println("main..."+x);
                }
        }
}


为何this.name = name;   得不到自定义的线程名 而需为 super(name);

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

10 个回复

倒序浏览
很简单的,以为你的线程类是没有gerName()的。
  1. class Test extends Thread
  2. {
  3.         private String name;
  4.         Test(String name)
  5.         {
  6.                 //super(name);
  7.                 this.name = name;
  8.         }
  9.         public void run()
  10.         {
  11.                 for (int x=0; x<60; x++)
  12.                 {
  13.                         System.out.println(this.name+"-----"+x);//这里改下就好了。
  14.                 }
  15.         }
  16. }
  17. class  T1
  18. {
  19.         public static void main(String[] args)
  20.         {
  21.                 Test t1 = new Test("one-----");
  22.                 Test t2 = new Test("two+++++");
  23.                 t1.start();
  24.                 t2.start();

  25.                 for (int x=0; x<60; x++)
  26.                 {
  27.                         System.out.println("main..."+x);
  28.                 }
  29.         }
  30. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

回复 使用道具 举报
线程名字跟对象名字不是一个名字,this.name = name//表示你对象里有个name变量,当对象创建时构造函数将name变量赋值为传进来的参数String name.
给你发一下线程构造函数源代码(发几个构造函数给你),你看一下Thread构造函数的构造方法知道给线程起名需要调用 init()函数。代码如下:
  1. public Thread() {
  2.         init(null, null, "Thread-" + nextThreadNum(), 0);
  3.     }

  4.     /**
  5.      * Allocates a new {@code Thread} object. This constructor has the same
  6.      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
  7.      * {@code (null, target, gname)}, where {@code gname} is a newly generated
  8.      * name. Automatically generated names are of the form
  9.      * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
  10.      *
  11.      * @param  target
  12.      *         the object whose {@code run} method is invoked when this thread
  13.      *         is started. If {@code null}, this classes {@code run} method does
  14.      *         nothing.
  15.      */
  16.     public Thread(Runnable target) {
  17.         init(null, target, "Thread-" + nextThreadNum(), 0);
  18.     }

  19.     /**
  20.      * Allocates a new {@code Thread} object. This constructor has the same
  21.      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
  22.      * {@code (group, target, gname)} ,where {@code gname} is a newly generated
  23.      * name. Automatically generated names are of the form
  24.      * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
  25.      *
  26.      * @param  group
  27.      *         the thread group. If {@code null} and there is a security
  28.      *         manager, the group is determined by {@linkplain
  29.      *         SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}.
  30.      *         If there is not a security manager or {@code
  31.      *         SecurityManager.getThreadGroup()} returns {@code null}, the group
  32.      *         is set to the current thread's thread group.
  33.      *
  34.      * @param  target
  35.      *         the object whose {@code run} method is invoked when this thread
  36.      *         is started. If {@code null}, this thread's run method is invoked.
  37.      *
  38.      * @throws  SecurityException
  39.      *          if the current thread cannot create a thread in the specified
  40.      *          thread group
  41.      */
  42.     public Thread(ThreadGroup group, Runnable target) {
  43.         init(group, target, "Thread-" + nextThreadNum(), 0);
  44.     }

  45.     /**
  46.      * Allocates a new {@code Thread} object. This constructor has the same
  47.      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
  48.      * {@code (null, null, name)}.
  49.      *
  50.      * @param   name
  51.      *          the name of the new thread
  52.      */
  53.     public Thread(String name) {
  54.         init(null, null, name, 0);
  55.     }
复制代码

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

回复 使用道具 举报
Thread类中的构造方法Thread(String name)是指分配一个新的指定名称为name的Thread对象。
如果没有super(name)的话,只是子类进行构造函数初始化了,父类并没有进行构造函数初始化,
也就是子类分配了一个自动生成的名称的Thread对象,而不是自定义的线程名的Thread对象。
个人理解,希望对你有帮助。

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

回复 使用道具 举报

class Test extends Thread
{
        private String name;//这并不是线程的名称,只是你定义的一个name属性而以。
        Test(String name)
        {
                //super(name);        //super()这是调用父类Thread类的构造函数给你的线程取一个字定义名称;
                this.name = name;
        }
        public void run()
        {
                for (int x=0; x<60; x++)
                {
                       // System.out.println(this.getName()+"-----"+x);//this 表示当前对象,这里表示当前线程,而this.getName()得到的是线程的默认名称,而不是你定义的那个name属性。
                        System.out.println(name+"-------"+x);//这样就可以了,
                       
                }
        }
}
class  ThreadTest
{
        public static void main(String[] args)
       {
                Test t1 = new Test("one-----");
                Test t2 = new Test("two+++++");
                t1.start();
                t2.start();

               for (int x=0; x<60; x++)
                {
                        System.out.println("main..."+x);
                }
        }
}

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

回复 使用道具 举报
张栓紧 发表于 2013-3-16 17:51
很简单的,以为你的线程类是没有gerName()的。

继承Thread类  不是可以拿到Thread中的getName()  吗
回复 使用道具 举报
騛鹏 中级黑马 2013-3-16 18:32:23
7#
黑马李超 发表于 2013-3-16 18:00
Thread类中的构造方法Thread(String name)是指分配一个新的指定名称为name的Thread对象。
如果没有super(na ...

没有super(name);  不是还有默认的super(); 了嘛
回复 使用道具 举报
騛鹏 发表于 2013-3-16 18:25
继承Thread类  不是可以拿到Thread中的getName()  吗

是可以,但是你得到的是父类的名字,。。
回复 使用道具 举报
騛鹏 中级黑马 2013-3-16 18:40:40
9#
张子凯 发表于 2013-3-16 17:58
线程名字跟对象名字不是一个名字,this.name = name//表示你对象里有个name变量,当对象创建时构造函数将na ...

既然super(name);  可以拿到线程名字
那么说明父类Thread中对应的构造函数 Thread(String name){}  可以改变线程的名字  这其中是怎么实现的呢
回复 使用道具 举报
个人理解  我们都知道 super和 this都 是只能放在构造函数的第一行的,用了this就不能用super。 你用this的时候系统自动不调用super了,自然就没有赋给父类name值。然后你下面调用this.getName()方法 这个确实是继承了父类的方法,但是你上面并没有对父类进行初始化,是吧。然后系统就自动分配了一个线程名。 不知道理解的对不对 欢迎指正
回复 使用道具 举报
騛鹏 发表于 2013-3-16 18:40
既然super(name);  可以拿到线程名字
那么说明父类Thread中对应的构造函数 Thread(String name){}  可以 ...

给你瞅瞅构造函数,一看就知道构造函数中是调用别的函数。代码:
  1. /**
  2.      * Allocates a new {@code Thread} object. This constructor has the same
  3.      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
  4.      * {@code (null, null, name)}.
  5.      *
  6.      * @param   name
  7.      *          the name of the new thread
  8.      */
  9.     public Thread(String name) {
  10.         init(null, null, name, 0);//这是Thread的构造函数,它调用init();,其中name作为参数传进去。
  11.     }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马