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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© NO? 中级黑马   /  2014-3-30 22:29  /  1259 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class  Teste
{
        public static void main(String[] args)
        {   new Thread(new NewThread("a")).start();
            new Thread(new NewThread("b")).start();
                for (int x=0;x<100 ;x++ )
                {
                        System.out.println(x+"    "+Thread.currentThread().getName());
                }
        }
}
class  NewThread implements Runnable
{
        private String name;
                public NewThread(String name)
                {
                  this.name=name;
            }
        public void run()
        {   
                for (int x=0;x<100 ;x++ )
                {
                        System.out.println(x+"       "+Thread.currentThread().getName());
                }
        }
}


QQ截图20140330221913.png (2.44 KB, 下载次数: 7)

QQ截图20140330221913.png

评分

参与人数 1技术分 +1 收起 理由
滔哥 + 1

查看全部评分

5 个回复

倒序浏览
线程里面有setName()的方法,不知道你为什么要在构造函数中传一个name的值,这里不是你取个名字叫name,线程的名字就会自己改成名字的。
  1. public class Test2 {
  2.        
  3.                 public static void main(String[] args)
  4.                 {   
  5.                         Thread t1=new Thread(new NewThread("a"));
  6.                         t1.setName("a");
  7.                         t1.start();
  8.                     Thread t2=new Thread(new NewThread("b"));
  9.                     t2.setName("b");
  10.                         t2.start();
  11.                         for (int x=0;x<100 ;x++ )
  12.                         {
  13.                                 System.out.println(x+"    "+Thread.currentThread().getName());
  14.                         }
  15.                 }
  16.         }
  17.         class  NewThread implements Runnable
  18.         {
  19.                 private String name;
  20.                         public NewThread(String name)
  21.                         {
  22.                           this.name=name;
  23.                     }
  24.                         
  25.                 public void run()
  26.                 {   
  27.                         for (int x=0;x<100 ;x++ )
  28.                         {
  29.                                 System.out.println(x+"       "+Thread.currentThread().getName());
  30.                         }
  31.                 }
  32.         }
复制代码

评分

参与人数 1技术分 +1 收起 理由
滔哥 + 1

查看全部评分

回复 使用道具 举报
  1. class  NewThread implements Runnable
  2. {
  3.         private String name;
  4.                 public NewThread(String name)
  5.                 {
  6.                   this.name=name;
  7.             }
  8.         public void run()
  9.         {   
  10.                 for (int x=0;x<100 ;x++ )
  11.                 {
  12.                         System.out.println(x+"       "+<u>Thread.currentThread().getName()</u>);
  13.                 }
  14.         }
  15. }
复制代码
在你自己的线程类里面,想用构造函数给线程起名字,但加下划线这句你调用的依然是系统默认的线程名称,你设置的name根本就没有使用。
要修改线程名称,就得用你的线程对象老调用setName()方法才能改线程名称,我将你的main方法修改了下,可以实现你的要求了。
  1. public static void main(String[] args)
  2.         {   Thread t1 = new Thread(new NewThread());
  3.                 Thread t2 = new Thread(new NewThread());
  4.                 t1.setName("a");
  5.                 t2.setName("b");               
  6.                 t1.start();
  7.                 t2.start();
  8.                 for (int x=0;x<100 ;x++ )
  9.                 {
  10.                         System.out.println(x+"    "+Thread.currentThread().getName());
  11.                 }
  12.         }
复制代码



评分

参与人数 1技术分 +1 收起 理由
滔哥 + 1

查看全部评分

回复 使用道具 举报
应该使用这个构造函数Thread(Runnable target, String name) ,new Thread(new NewThread(),"a").start(); 同时把NewThread的构造函数去掉

评分

参与人数 1技术分 +1 收起 理由
滔哥 + 1

查看全部评分

回复 使用道具 举报
  1. public static void main(String[] args)
  2.         {  
  3.                          Thread t1=new Thread(new NewThread("a"));//创建线程t1
  4.                          Thread t2= new Thread(new NewThread("b"));//创建线程t2
  5.                          t1.setName("a");//将线程Thread-0 的名字设置为“a”
  6.                          t2.setName("b");//将线程Thread-1 的名字设置为“b”
  7.                          t1.start();
  8.                          t2.start();
  9.                          
  10.                          for (int x=0;x<100 ;x++ )
  11.                 {
  12.                         System.out.println(x+"    "+Thread.currentThread().getName());
  13.                 }
  14.                          
  15.                
  16.         }
复制代码

修正代码如上:
因为线程有设置名字的方法setName(),只要调用线程setName()方法就可以设置线程的名字

评分

参与人数 1技术分 +1 收起 理由
itpower + 1

查看全部评分

回复 使用道具 举报
楼主,直接看我的解释,
我看完了楼上所有的解释,
虽然都对,但不利于理解。



首先,你这个问题完全可以简化为两个线程,一是main方法的主线程,二是自己new的线程。
然后根据你的问题,结合自定义线程的两种方式,来看你理解有误的地方。

你在程序中,用的是implements的方式,
估计你觉得重写NewThread类的构造方法,
就能够输出你所自定义的名字。
但你要注意, new Thread(new NewThread("a")).start();这一句,
真正运行的线程是红色的那个,NewThread所产生的对象,
只是作为target提供run方法被调用而已。

所以,按照你原来的想法,应该用API提供的构造方法:Thread(Runnable  target,String  name)
程序如下:

  1. public class Test {
  2.         public static void main(String[] args) {
  3.                 Thread t1 = new Thread(new NewThread(),"A");
  4.                 Thread t2 = new Thread(new NewThread(),"B");
  5.                 t1.start();
  6.                 t2.start();
  7.                 for (int x = 0; x < 100; x++) {
  8.                         System.out.println(x + "    " + Thread.currentThread().getName());
  9.                 }
  10.         }
  11. }

  12. class NewThread implements Runnable {

  13.         public void run() {
  14.                 for (int x = 0; x < 100; x++) {
  15.                         System.out.println(x + "       " + Thread.currentThread().getName());
  16.                 }
  17.         }
  18. }
复制代码

再来说说继承Thread类的创建方式,有了上面这参考,
这个方式就相对好办了,只要在你自定义的NewThread中,
调用Thread(String name)这个构造方法重写就可以了,
程序如下:

  1. public class Test {
  2.         public static void main(String[] args) {
  3.                 NewThread A = new NewThread("A");
  4.                 NewThread B = new NewThread("B");
  5.                 A.start();
  6.                 B.start();
  7.                 for (int x = 0; x < 100; x++) {
  8.                         System.out.println(x + "    " + Thread.currentThread().getName());
  9.                 }
  10.         }
  11. }

  12. class NewThread extends Thread {
  13.        
  14.         public NewThread(String name){
  15.                 super(name);
  16.         }
  17.        
  18.         public void run() {
  19.                 for (int x = 0; x < 100; x++) {
  20.                         System.out.println(x + "       " + Thread.currentThread().getName());
  21.                 }
  22.         }
  23. }
复制代码

只要看清楚,就不容易出错了。



评分

参与人数 1技术分 +1 收起 理由
ily521125 + 1 很给力!

查看全部评分

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