Java Thread多线程的start()和run()

1.start()方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码:
 
通过调用Thread类的start()方法来启动一个线程,
这时此线程是处于就绪状态,
并没有运行。
然后通过此Thread类调用方法run()来完成其运行操作的,
这里方法run()称为线程体,
它包含了要执行的这个线程的内容,
Run方法运行结束,
此线程终止,
而CPU再运行其它线程,

2.run()方法当作普通方法的方式调用,程序还是要顺序执行,还是要等待run方法体执行完毕后才可继续执行下面的代码:
而如果直接用Run方法,
这只是调用一个方法而已,
程序中依然只有主线程--这一个线程,
其程序执行路径还是只有一条,
这样就没有达到写线程的目的。
通过implements Runnable接口来实现多线程

示例代码:

 代码如下 复制代码

package com;

public class TestRunnable implements Runnable{
 private int num = 0;
 public static void main(String[] args) {
  for(int i=0; i<5; i++){
   new Thread(new TestRunnable(i)).start();
  }
 }

 public TestRunnable(int num) {
  this.num = num;
 }

 //实现方法
 public void run(){
  System.out.println("线程:"+this.num);
 }
}

上例打印出来的结果是无序的:
1 4 3 2 0

也可能是别的顺序,通过Thread类调用start,真正实现了多线程,不用等待run方法执行完毕再起新线程。

如果将

 代码如下 复制代码

new Thread(new TestRunnable(i)).start()

改成

new Thread(new TestRunnable(i)).run(),

则打印出来的结果是由顺序的,也就是:

0 1 2 3 4

通过Thread类直接调用run方法,实际上就是调用TestRunnable类的run方法,查看Thread类的run方法:

 代码如下 复制代码

 

public void run() {
    if (target != null) {
        target.run();
    }
}

发现Thread类run方法就是调用了TestRunnable的run(),所以通过

 代码如下 复制代码

new Thread(new TestRunnable(i)).run()

并不是多线程,其执行线程还是主线程。

 

下面再将Thread类的start方法贴出来,可与上面Thread类的run方法做个对比:

 代码如下 复制代码

 /**
     * Causes this thread to begin execution; the Java Virtual Machine
     * calls the <code>run</code> method of this thread.
     * <p>
     * The result is that two threads are running concurrently: the
     * current thread (which returns from the call to the
     * <code>start</code> method) and the other thread (which executes its
     * <code>run</code> method).
     * <p>
     * It is never legal to start a thread more than once.
     * In particular, a thread may not be restarted once it has completed
     * execution.
     *
     * @exception  IllegalThreadStateException  if the thread was already
     *               started.
     * @see        #run()
     * @see        #stop()
     */
    public synchronized void start() {
        /**
         * This method is not invoked for the main method thread or "system"
         * group threads created/set up by the VM. Any new functionality added
         * to this method in the future may have to also be added to the VM.
         *
         * A zero status value corresponds to state "NEW".
         */
        if (threadStatus != 0)
            throw new IllegalThreadStateException();

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

        boolean started = false;
        try {
            start0();
            started = true;
        } finally {
            try {
                if (!started) {
                    group.threadStartFailed(this);
                }
            } catch (Throwable ignore) {
                /* do nothing. If start0 threw a Throwable then
                  it will be passed up the call stack */
            }
        }
    }

时间: 2024-07-31 04:29:01

Java Thread多线程的start()和run()的相关文章

Java Thread多线程全面解析_java

多线程是java中很重要的知识点,在此小编给大家总结Java Thread多线程,非常有用,希望大家可以掌握哦. 一.线程的生命周期及五种基本状态 关于Java中线程的生命周期,首先看一下下面这张较为经典的图: 上图中基本上囊括了Java中多线程各重要知识点.掌握了上图中的各知识点,Java中的多线程也就基本上掌握了.主要包括: Java线程具有五种基本状态 新建状态(New):当线程对象对创建后,即进入了新建状态,如:Thread t = new MyThread(); 就绪状态(Runnab

Java Thread多线程详解及用法解析_java

最全面的java多线程用法解析,如果你对Java的多线程机制并没有深入的研究,那么本文可以帮助你更透彻地理解Java多线程的原理以及使用方法. 1.创建线程 在Java中创建线程有两种方法:使用Thread类和使用Runnable接口.在使用Runnable接口时需要建立一个Thread实例.因此,无论是通过Thread类还是Runnable接口建立线程,都必须建立Thread类或它的子类的实例.Thread构造函数: public Thread( ); public Thread(Runnab

java Thread 多线程_java

Thread 创建线程的两种方法: 1.定义类继承Thread类,覆写类中的run方法,调用类对象的start方法,start方法启动线程,调用run方法.Thread类用于描述线程:该类定义一个功能run,用于存储线程要运行的代码. 2.定义类实现Runnable接口,覆盖Runnable接口中的方法,通过Thread类建立线程对象,将Runnable接口的子类对象作为实际参数传递给Thread类的构造函数,调用Thread类的start方法开启线程,线程会调用Runnable接口子类中的ru

java实现多线程的两种方式继承Thread类和实现Runnable接口的方法_java

实现方式和继承方式有什么区别呢? *区别: *继承Thread:线程代码存放在Thread子类run方法中 *实现Runnable:线程代码存放在接口的子类的run方法中 *实现方式的好处:避免了单继承的局限性 *在定义线程时,建议使用实现方式,当然如果一个类没有继承父类,那么也可以通过继承Thread类来实现多线程 *注意:Runnable接口没有抛出异常,那么实现它的类只能是try-catch不能throws *Java对多线程的安全问题提供了专业的解决方式就是同步代码块synchroniz

Java Thread Programming 1.8.2 - Inter-thread Communication

  Missed Notification A missed notification occurs when threadB tries to notify threadA, but threadA is not yet waiting for the notification. In a multithreaded environment like Java, you don't have much control over which thread runs and for how lon

用Java实现多线程服务器程序

---- 摘要:在Java出现之前,编写多线程程序是一件烦琐且伴随许多不安全因素的事情.利用Java,编写安全高效的多线程程序变得简单,而且利用多线程和Java的网络包我们可以方便的实现多线程服务器程序. ---- Java是伴随Internet的大潮产生的,对网络及多线程具有内在的支持,具有网络时代编程语言的一切特点.从Java的当前应用看,Java主要用于在Internet或局域网上的网络编程,而且将Java作为主流的网络编程语言的趋势愈来愈明显.实际工作中,我们除了使用商品化的服务器软件外

Java开发多线程同步技巧

在编写一个类时,如果该类中的代码可能运行于多线程环境下,那么就要考虑同步的问题.在Java中内置了语言级的同步原语--synchronized,这也大大简化了Java中多线程同步的使用. 我们首先编写一个非常简单的多线程的程序,是模拟银行中的多个线程同时对同一个储蓄账户进行存款.取款操作的. 在程序中我们使用了一个简化版本的Account类,代表了一个银行账户的信息.在主程序中我们首先生成了1000个线程,然后启动它们,每一个线程都对John的账户进行存100元,然后马上又取出100元.这样,对

JAVA的多线程浅析

一 JAVA 语言的来源.及特点 在这个高速信息的时代,商家们纷纷把信息.产品做到Internet国际互连网页上.再这些不寻常网页的背后,要属功能齐全.安全可靠的编程语言,Java是当之无愧的.Java是由Sun Microsystem开发的一种功能强大的新型程序设计语言.是与平台无关的编程语言.它是一种简单的.面象对象的.分布式的.解释的.键壮的.安全的.结构的中立的.可移植的.性能很优异的.多线程的.动态的.语言. Java自问世以后,以其编程简单.代码高效.可移植性强,很快受到了广大计算机

java se 多线程资源共享问题

问题描述 java se 多线程资源共享问题 class bread{ int num = 0; public synchronized void makeBreand(){ num++; this.notify(); } public synchronized void sale(){ while(num==0){ try{ this.wait(); System.out.println("暂时无面包,等待"); }catch(InterruptedException e){e.pr