`
CindyLiao
  • 浏览: 12612 次
  • 性别: Icon_minigender_2
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

【转】Thread的run()与start()的区别

阅读更多

【源地址】http://blog.csdn.net/tornado886/article/details/4524346

java中thread的start()和run()的区别:

1.start()方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码:

通过调用Thread类的start()方法来启动一个线程,
这时此线程是处于就绪状态,
并没有运行。
然后通过此Thread类调用方法run()来完成其运行操作的,
这里方法run()称为线程体,
它包含了要执行的这个线程的内容,
Run方法运行结束,
此线程终止,
而CPU再运行其它线程,

 

2.run()方法当作普通方法的方式调用,程序还是要顺序执行,还是要等待run方法体执行完毕后才可继续执行下面的代码:

而如果直接用Run方法,
这只是调用一个方法而已,
程序中依然只有主线程--这一个线程,
其程序执行路径还是只有一条,
这样就没有达到写线程的目的。

 

举例说明一下:

记住:线程就是为了更好地利用CPU,
提高程序运行速率的!

public class TestThread1{
public static void main(String[] args){
Runner1 r=new Runner1();
//r.run();//这是方法调用,而不是开启一个线程
Thread t=new Thread(r);//调用了Thread(Runnable target)方法。且父类对象变量指向子类对象。
t.start();

for(int i=0;i<100;i++){
System.out.println("进入Main Thread运行状态");
System.out.println(i);
}
}
}
class Runner1 implements Runnable{ //实现了这个接口,jdk就知道这个类是一个线程
public void run(){

for(int i=0;i<100;i++){
System.out.println("进入Runner1运行状态");
System.out.println(i);
}
}
}

同时摘取一段外文网站论坛上的解释:
Why do we need start() method in Thread class? In Java API description for Thread class is written : "Java Virtual Machine calls the run method of this thread..".

Couldn't we call method run() ourselves, without doing double call: first we call start() method which calls run() method? What is a meaning to do things such complicate?



 

There is some very small but important difference between using start() and run() methods. Look at two examples below:

Example one:

Code:

Thread one = new Thread();
Thread two = new Thread();
one.run();
two.run();

Example two:

Code:

Thread one = new Thread();
Thread two = new Thread();
one.start();
two.start();

The result of running examples will be different.

In Example one the threads will run sequentially: first, thread number one runs, when it exits the thread number two starts.

In Example two both threads start and run simultaneously.

Conclusion: the start() method call run() method asynchronously (does not wait for any result, just fire up an action), while we run run() method synchronously - we wait when it quits and only then we can run the next line of our code.

分享到:
评论

相关推荐

    Android:Handler的post()方法和Thread的start()方法执行Thread的run()方法的区别

    t.start(); setContentView(R.layout.activity_main); System.out.println("Activity--&gt;"+ Thread.currentThread().getId()); System.out.println("Activity--&gt;"+ Thread.currentThread().getName()); }...

    Java多线程——Thread类中run()方法和start()方法的区别.docx

    通过调用Thread类的start()方法来启动一个线程,这时此线程处于就绪(可运行)状态,并没有运行,一旦得到spu时间片,就开始执行run()方法,这里方法run()称为线程体,它包含了要执行的这个线程的内容,Run方法运行...

    Thread中start()和run()的区别_动力节点Java学院整理

    start() : 它的作用是启动一个新线程,新线程会执行相应的run()方法。start()不能被重复调用。 run() : run()就和普通的成员方法一样,可以被重复调用。单独调用run()的话,会在当前线程中执行run(),而并不会启动...

    Java Thread 的 run() 与 start() 的区别.docx

    通过调用Thread类的start()方法来启动一个线程,这时此线程处于就绪(可运行)状态,并没有运行,一旦得到cpu时间片,就开始执行run()方法,这里方法 run()称为线程体,它包含了要执行的这个线程的内容,Run方法运行...

    java中thread线程start和run的区别

    主要介绍了java中thread线程start和run的区别,run()是Runnable接口中定义的一个方法,是为了让客户程序员在这个方法里写自己的功能代码的。直接调用和普通的类调用自己的成员方法是没有任何区别的

    Java Thread中start()和run()的区别_动力节点Java学院整理

    start() : 它的作用是启动一个新线程,新线程会执行相应的run()方法。start()不能被重复调用。而run() : run()就和普通的...下面通过示例代码给大家介绍了Java Thread中start()和run()的区别,感兴趣的朋友一起看看吧

    java thread start()和run()方法简析

    本文以java中thread的start()和run()的区别做详细介绍, 需要了解跟多的朋友可以参考下

    Java中Thread.start()和Thread.run()的区别.docx

    Java是一种功能强大、可靠性高、跨平台的编程语言,适用于各种应用场景。它被广泛用于企业级应用开发、移动应用开发、云计算、大数据处理等领域,是一门值得学习和掌握的编程语言。

    java基本教程之Thread中start()和run()的区别 java多线程教程

    主要介绍了Thread中start()和run()的区别,Thread类包含start()和run()方法,它们的区别是什么?下面将对此作出解答

    Thread实现多线程

    继承Thread类: 1必须重写run 方法:里面放置的实际的线程体 2 启动线程: 3创建Thread对象 4调用Thread对象的start 方法启动线程

    ThreadPriority.java

    class ThreadDemo extends Thread { public thread1(String s) { super(s); } public void run() { while(true) System.out.println(getName()); } } class thread2 extends ... thread1.start(); thread2.start(); } }

    Python run()函数和start()函数的比较和差别介绍

    1 使用run()方法启动线程,它打印的线程名是MainThread,也就是主线程。 import threading,time def worker(): count = 1 while True: if count &gt;= 4: break time.sleep(1) count += 1 print(“thread name = {}”....

    认识Thread和Runnable

    认识Thread和Runnable 认识Thread的start和run 线程状态说明

    创建线程的三种方式(Thread、Runnable、Callable).docx

    3. 每个线程都是通过某个特定的Thread对象所对应的方法run( )来完成其操作的,方法run( )称为线程体。 4. 通过调用Thread类的start()方法来启动一个线程(只是将线程由新生态转为就绪态,而不是运行态)。

    判断Threading.start新线程是否执行完毕的实例

    新写自己的Threading类 class MyThread(threading.Thread):#我的Thread类 判断流程结束没 用于os shell命令是否执行判断 ... # 调用start自动执行的函数 def run(self): self.result = self.func() self.resul

    关于Thread -----使用线程更新进度组件

    本示例 演示采用线程的方式在后台执行任务 要在Android系统中创建和启动线程,与传统的Java程序相同,首先要创建一个Thread对象,使用Thread类的start方法启动线程 启动后将会执行Runnable接口的run方法

    java多线程示例

    Thread本质上也是一个实现了Runnable的实例,他代表一个线程的实例,并且启动线程的唯一方法就是通过Thread类的start方法。 2.实现Runnable接口,并实现该接口的run()方法.创建一个Thread对象,用实现的Runnable接口...

    Qt5中两种线程操作的方法 demo代码

    QThread::run()是线程的入口 相当于 main函数一样 创建的线程通过调用start()来执行run(); run() 是一个虚函数 通过子类实现方法 通过moveToThread()函数来实现 作用:将某一个对象从当前的线程中推到另一个线程中,...

    多线程火车售票

    public void run() { while(true) { if(num&gt;0)//:如果有票! System.out.println(Thread.currentThread().getName()+"线程出售的是第"+ num-- +"张火车票!"); } } public static void main(String...

    Java语言多线程编程精要之实现线程

    Thread类是一个具体的类,即不是抽象类,该类封装了线程的行为。要创建一个线程,程序员...而是必须调用Thread的start()函数,该函数再调用run()。Runnable接口只有一个函数run(),此函数必须由实现了此接口的类实现。

Global site tag (gtag.js) - Google Analytics