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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

代码如下:

  1. class Test extends Thread
  2. {
  3.         private String name;
  4.         Test(String name)
  5.         {
  6.                 this.name=name;
  7.         }
  8.         public void run()
  9.         {
  10.                 for(int x=0;x<60;x++)
  11.                 {
  12.                         System.out.println(this.getName()+"run-------"+x);
  13.                 }
  14.         }
  15. }

  16. class ThreadTest
  17. {
  18.         public static void main(String[] args)
  19.         {
  20.                 Test t1=new Test("one");
  21.                 Test t2=new Test("two");
  22.                 t1.start();
  23.                 t2.start();
  24.                 for(int x=0;x<60;x++)
  25.                 {
  26.                         System.out.println("main--------"+x);
  27.                 }
  28.         }
  29. }
复制代码

运行结果如下:

  1. E:\sourcefile>java ThreadTest
  2. main--------0
  3. Thread-1run-------0
  4. Thread-0run-------0
  5. Thread-1run-------1
  6. main--------1
  7. Thread-1run-------2
  8. Thread-0run-------1
  9. Thread-1run-------3
  10. Thread-1run-------4
  11. main--------2
  12. Thread-1run-------5
  13. Thread-0run-------2
  14. Thread-1run-------6
  15. main--------3
复制代码

说明:线程都有自己的默认名称,即Thread-x,x从0开始,可以在代码中用this.getName()方法获得。getName是Test从父类Thread中继承的方法。
有人说,Thread-0等名字太难看了,能否自己定义名字,答案是可以,请看下面代码:
代码一

  1. class Test extends Thread
  2. {
  3.         //private String name;
  4.         Test(String name)
  5.         {
  6.                 //this.name=name;
  7.                 super(name);
  8.         }
  9.         public void run()
  10.         {
  11.                 for(int x=0;x<60;x++)
  12.                 {
  13.                         System.out.println(this.getName()+"run-------"+x);
  14.                 }
  15.         }
  16. }

  17. class ThreadTest
  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. E:\sourcefile>java ThreadTest
  2. main--------0
  3. tworun-------0
  4. onerun-------0
  5. tworun-------1
  6. main--------1
  7. tworun-------2
  8. onerun-------1
  9. tworun-------3
  10. main--------2
  11. tworun-------4
  12. onerun-------2
  13. tworun-------5
  14. main--------3
  15. tworun-------6
  16. onerun-------3
  17. tworun-------7
复制代码
上面代码为运行结果的部分,表明可以自定义线程的名称。
代码二

  1. class Test extends Thread
  2. {
  3.         //private String name;
  4.         Test(String name)
  5.         {
  6.                 //this.name=name;
  7.                 super(name);
  8.         }
  9.         public void run()
  10.         {
  11.                 for(int x=0;x<60;x++)
  12.                 {
  13.                         System.out.println(Thread.currentThread().getName()+"run-------"+x);
  14.                         //System.out.println(this.getName()+"run-------"+x);
  15.                 }
  16.         }
  17. }

  18. class ThreadTest
  19. {
  20.         public static void main(String[] args)
  21.         {
  22.                 Test t1=new Test("one");
  23.                 Test t2=new Test("two");
  24.                 t1.start();
  25.                 t2.start();
  26.                 for(int x=0;x<60;x++)
  27.                 {
  28.                         System.out.println("main--------"+x);
  29.                 }
  30.         }
  31. }
复制代码
代码一与代码二运行结果相同,表明Thread.currentThread()==this,但是前者更为标准。
总结:
1、设置线程名称的方法:setName()方法或者构造函数。
2、每个线程在运行时都会在栈内存中分配空间,所以同一段run方法代码,在不同的线程中会有自己不同的一份。

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马