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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 尹丽峰 高级黑马   /  2013-4-26 10:19  /  2978 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 尹丽峰 于 2013-4-27 20:31 编辑
  1. public class ThreadDemo extends Thread
  2. {


  3. private int a;
  4. public void start()
  5. {
  6. super.start();
  7. }
  8. public void run(){
  9. System.out.println("线程"+currentThread().getName()+"启动");
  10. for(a=0;a<40;a++){
  11. System.out.println(currentThread().getName()+":"+a);
  12. }
  13. }
  14. public static void main(String[] args) {
  15. ThreadDemo m1 = new ThreadDemo();
  16. m1.start();
  17. ThreadDemo m2 = new ThreadDemo();
  18. m2.start();
  19. for(int x=0;x<40;x++)
  20. {
  21. System.out.println("main线程:"+x);
  22. }
  23. }
  24. }
复制代码
  1. public class Machine3 extends Thread{
  2. private int a;
  3. public void start(){
  4. run();
  5. }
  6. public void run(){
  7. for(a=0;a<40;a++){
  8. System.err.println(currentThread().getName()+":"+a);
  9. }
  10. }
  11. public static void main(String[] args) {
  12. Machine3 m1 = new Machine3();
  13. m1.start(); //试一下去掉start方法后的结果
  14. Machine3 m2 = new Machine3();
  15. m2.start();
  16. }
  17. }
复制代码
重写start方法能用第二段吗?

评分

参与人数 2技术分 +2 收起 理由
袁梦希 + 1 很给力!
田磊阳 + 1

查看全部评分

8 个回复

倒序浏览
本帖最后由 杨玄文 于 2013-4-26 10:32 编辑
  1. public synchronized void start() {
  2.         /**
  3.          * This method is not invoked for the main method thread or "system"
  4.          * group threads created/set up by the VM. Any new functionality added
  5.          * to this method in the future may have to also be added to the VM.
  6.          *
  7.          * A zero status value corresponds to state "NEW".
  8.          */
  9.         if (threadStatus != 0)
  10.             throw new IllegalThreadStateException();

  11.         /* Notify the group that this thread is about to be started
  12.          * so that it can be added to the group's list of threads
  13.          * and the group's unstarted count can be decremented. */
  14.         group.add(this);

  15.         boolean started = false;
  16.         try {
  17.             start0();
  18.             started = true;
  19.         } finally {
  20.             try {
  21.                 if (!started) {
  22.                     group.threadStartFailed(this);
  23.                 }
  24.             } catch (Throwable ignore) {
  25.                 /* do nothing. If start0 threw a Throwable then
  26.                   it will be passed up the call stack */
  27.             }
  28.         }
  29.     }

  30.     private native void start0();

  31.     /**
  32.      * If this thread was constructed using a separate
  33.      * <code>Runnable</code> run object, then that
  34.      * <code>Runnable</code> object's <code>run</code> method is called;
  35.      * otherwise, this method does nothing and returns.
  36.      * <p>
  37.      * Subclasses of <code>Thread</code> should override this method.
  38.      *
  39.      * @see     #start()
  40.      * @see     #stop()
  41.      * @see     #Thread(ThreadGroup, Runnable, String)
  42.      */
  43.     @Override
  44.     public void run() {
  45.         if (target != null) {
  46.             target.run();
  47.         }
  48.     }
复制代码
start()的源代码附上,是调用了系统底层的strat0()方法,所以你这样重写应该是不可以的,会有很多安全隐患
被native修饰的方法就表示是系统底层的方法

评分

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

查看全部评分

回复 使用道具 举报
客gou你好,{:soso_e113:}

线程要执行的代码都保存在run方法中了,而start()方法是开启线程的,你却把线程要执行的代码,放到了开启线程的方法里。
前一个代码是调用父类的空的构造函数,所以线程开始了。后一个代码是调用了该线程要运行的代码。

有些个人观点在里面。
回复 使用道具 举报
袁梦希 发表于 2013-4-26 10:38
客gou你好,

线程要执行的代码都保存在run方法中了,而start()方法是开启线程的,你却把线程 ...

感动狗啊这贴考验黑马学员了,仔细看看 代码2和线程有关系吗?
回复 使用道具 举报
本帖最后由 袁梦希 于 2013-4-26 10:53 编辑
尹丽峰 发表于 2013-4-26 10:40
感动狗啊这贴考验黑马学员了,仔细看看 代码2和线程有关系吗?


我说有关系了么,你根本都没开启线程,也没调用start()方法   哪来的线程呢   我说的是主线程    哈哈哈哈。小样{:soso_e113:}
回复 使用道具 举报
袁梦希 发表于 2013-4-26 10:50
我说有关系了么,你根本都没开启线程,也没调用start()方法   哪来的线程呢   小样 ...

亲 你运行一下代码2 看看有没?看看启动没?
回复 使用道具 举报
尹丽峰 发表于 2013-4-26 10:53
亲 你运行一下代码2 看看有没?看看启动没?

第一个有运行线程了
但是第二个也运行线程
但是只有一个主线程而已...

我想问一下调用父类的start方法和调用本身的start方法有什么区别。
源码我是没看懂,求高人解释
回复 使用道具 举报
刘胜寒 发表于 2013-4-26 18:43
第一个有运行线程了
但是第二个也运行线程
但是只有一个主线程而已...

高级黑马真是名不虚传,感谢你的回答,你回答的完全正确,第二个没启动别的线程只是一个主线程在执行,super.start();是直接调用了父类的start方法,而第二段代码中的start只是一个start方法并没有重写父类的start方法

评分

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

查看全部评分

回复 使用道具 举报
尹丽峰 发表于 2013-4-26 20:43
高级黑马真是名不虚传,感谢你的回答,你回答的完全正确,第二个没启动别的线程只是一个主线程在执行,su ...

我没去黑马...
只是准备去黑马...
我想问一下第一个代码
调用父类的start和调用本身的start有什么区别...
但是运行第一个结果好像是没区别
回复 使用道具 举报
如果仍有问题,请继续追问,如果问题已解决,请将分类改为已解决,谢谢
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马