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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 木木赤赤 中级黑马   /  2013-11-26 10:01  /  1597 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 木木赤赤 于 2013-11-26 10:59 编辑

class Demo extends Thread
{
       publicvoid run()
       {
              for(intx=0; x<60;x++)
              System.out.println("demorun--"+x);
       }
}
class ThreadDemo
{
       publicstatic void main(String[] args)
       {
              Demod= new Demo();
              d.start();
              //d,run
              for(intx=0; x<60;x++)
              System.out.println("hello--"+x);
       }
}


直接调用d.run();方法,和通过d.start();调用run方法有什么区别,都会产生什么效果?

评分

参与人数 1技术分 +1 收起 理由
FFF + 1 赞一个!

查看全部评分

7 个回复

倒序浏览
直接调用run是不会开启多线程的   调用start方法  会调用系统底层开启多线程
所以要开启多线程 就要通过start方法
回复 使用道具 举报
Start用于开启线程,Run用于存储要运行的代码
1) start:
用start方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码。通过调用Thread类的start()方法来启动一个线程,这时此线程处于就绪(可运行)状态,并没有运行,一旦得到spu时间片,就开始执行run()方法,这里方法run()称为线程体,它包含了要执行的这个线程的内容,Run方法运行结束,此线程随即终止。
2) run:
run()方法只是类的一个普通方法,如果直接调用Run方法,程序中依然只有主线程这一个线程,其程序执行路径还是只有一条,还是要顺序执行,还是要等待run方法体执行完毕后才可继续执行下面的代码,这样就没有达到写线程的目的。

评分

参与人数 1技术分 +1 收起 理由
FFF + 1 赞一个!

查看全部评分

回复 使用道具 举报
直接调用Thread的子类Demo ,中的run方法,不会生成一个线程,他只是向平常的类之间的方法调用一样。而用new Demo().start(),这样会启动一个新线程,而且在运行时,jvm会在内部自动调用Demo中的run方法。
回复 使用道具 举报
  1. public class Test{

  2.         public static void main(String[] args){
  3.                 //这句话是你在创建一个Demo实例对象
  4.                 Demo d= new Demo();
  5.                 //。start()是java中开启一条新的运行线程的方法,从某中意义上讲是和主线程并行运行的
  6.                 d.start();
  7.                 //。run()则是普通的对象调用本身类型的类中定义的函数(方法)而没有开启一条新的工作线程是在主线程中按由上到下的顺序执行的
  8.                 d.run();
  9.                 //下面的代码自然不用说,也是在主线程中按由上至下的顺序执行的
  10.                 for(int x=0; x<60;x++)
  11.                         System.out.println("hello--"+x);
  12.         }
  13. }
  14. //自定义了一个线程
  15. class Demo extends Thread{
  16.         public void run(){
  17.                 for(int x=0; x<60;x++)
  18.                         System.out.println("demorun--"+x);
  19.         }
  20. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
FFF + 1 赞一个!

查看全部评分

回复 使用道具 举报
通过run方法不能创建线程,在Thread类源码中,run方法只是一个普通的方法,不能在JVM中创建线程,而是通过start()方法开启一个线程,start()方法内部是通过group.add()的方法将线程加入到group中,若group不为null,JVM就会启动group中的线程
附上源码:
  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.     }
复制代码

评分

参与人数 1黑马币 +6 收起 理由
FFF + 6 山寨

查看全部评分

回复 使用道具 举报
肖川 中级黑马 2013-11-26 10:45:34
7#
run方法里面放的是多线程需要执行的代码。规定就是这么写的。调用start方法会自动执行run方法里面的代码。没有听说过直接调用run方法这种。人家线程的运行机制就是这么设计的
回复 使用道具 举报
王雷1 中级黑马 2013-11-26 11:12:28
8#
最简单的说就是你调用run方法,就是调用run方法了,什么都没有,但是你调用start方法的意思是说,你要开启线程,并执行run方法。一个是调用普通方法,一个是开启线程并执行此特定方法。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马