Java中Runnable和Thread的区别

本文介绍了Java中实现多线程的两种主要方法:继承Thread类和实现Runnable接口。通过示例代码展示了这两种方法的具体应用,并对比了它们之间的区别与联系。文章还详细解释了为什么必须使用start()方法启动线程,以及如何利用Runnable接口实现资源共享。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  在Java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口;Thread类是在java.lang包中定义的。一个类只要继承了Thread类同时覆写了本类中的run()方法就可以实现多线程操作了,但是一个类只能继承一个父类,这是此方法的局限。

package org.thread.demo;  
class MyThread extends Thread {  
    private String name;  
    public MyThread(String name) {  
        super();  
        this.name = name;  
    }  
    public void run() {  
        for (int i = 0; i < 10; i++) {  
            System.out.println("线程开始:"+this.name+",i="+i);  
        }  
    }  
}  
package org.thread.demo;  
public class ThreadDemo01 {  
    public static void main(String[] args) {  
        MyThread mt1=new MyThread("线程a");  
        MyThread mt2=new MyThread("线程b");  
        mt1.run();  
        mt2.run();  
    }  
}
  但是,此时结果很有规律,先第一个对象执行,然后第二个对象执行,并没有相互运行。在JDK的文档中可以发现,一旦调用start()方法,则会通过JVM找到run()方法。下面启动start()方法启动线程:

package org.thread.demo;  
public class ThreadDemo01 {  
    public static void main(String[] args) {  
    MyThread mt1 = new MyThread("线程a");         
    MyThread mt2 = new MyThread("线程b");  
    mt1.start();  
    mt2.start();  
    }  
}

  这样程序可以正常完成交互式运行。那么为啥非要使用start()方法启动多线程呢?

  在JDK的安装路径下,src.zip是全部的Java源程序,通过此代码找到Thread类中的start()方法的定义,可以发现此方法中使用了private native void start0();其中native关键字表示可以调用操作系统的底层函数,那么这样的技术成为JNI技术(java Native Interface)。

Runnable接口

  在实际开发中一个多线程的操作很少使用Thread类,而是通过Runnable接口完成。

package org.runnable.demo;  
class MyThread implements Runnable {  
    private String name;  
    public MyThread(String name) {  
        this.name = name;  
    }
    public void run() {  
        for (int i = 0; i < 100; i++) {  
            System.out.println("线程开始:" + this.name + ", i = " + i);  
        }  
    }  
}

  但是在使用Runnable定义的子类中没有start()方法,只有Thread类中才有。此时观察Thread类,有一个构造方 法:public Thread(Runnable targer)此构造方法接受Runnable的子类实例,也就是说可以通过Thread类来启动Runnable实现的多线程。(start()可以协 调系统的资源):

package org.runnable.demo;  
import org.runnable.demo.MyThread;  
public class ThreadDemo01 {  
    public static void main(String[] args) {  
        MyThread mt1 = new MyThread("线程a");  
        MyThread mt2 = new MyThread("线程b");  
        new Thread(mt1).start();  
        new Thread(mt2).start();  
    }  
}

两种实现方式的区别和联系:

  在程序开发中只要是多线程肯定永远以实现Runnable接口为主,因为实现Runnable接口相比继承Thread类有如下好处:

  避免点继承的局限,一个类可以实现多个接口。

  适合于资源的共享。

以卖卡程序为例,通过Thread类完成:

package org.demo.dff;  
class MyThread extends Thread {  
    private int ticket = 10;   
    public void run(){  
        for(int i = 0; i < 20; i++) {  
            if(this.ticket > 0) {  
                System.out.println("卖卡:ticket"+this.ticket--);  
            }  
        }  
    }  
}

下面通过三个线程对象,同时卖卡:

package org.demo.dff;  
public class ThreadTicket {  
    public static void main(String[] args) {  
        MyThread mt1 = new MyThread();  
        MyThread mt2 = new MyThread();  
        MyThread mt3 = new MyThread();  
        mt1.start();// 每个线程都各卖了10张,共卖了30张卡 
        mt2.start();// 但实际只有10张卡,每个线程都卖自己的卡
        mt3.start();// 没有达到资源共享  
    }  
}

如果用Runnable就可以实现资源共享,下面看例子:

package org.demo.runnable;  
class MyThread implements Runnable{  

    private int ticket = 10;  
    public void run() {  
        for (int i = 0; i < 20; i++) {  
            if (this.ticket > 0) {  
                System.out.println("卖卡:ticket"+this.ticket--);  
            }  
        }  
    }  
}  
package org.demo.runnable;  
public class RunnableTicket {  
public static void main(String[] args) {  
    MyThread mt = new MyThread();  
    new Thread(mt).start();// 同一个mt,但是在Thread中就不可以,如果用同一  
    new Thread(mt).start();// 个实例化对象mt,就会出现异常  
    new Thread(mt).start();  
    }  
};

虽然现在程序中有三个线程,但是一共卖了10张卡,也就是说使用Runnable实现多线程可以达到资源共享目的。

  Runnable接口和Thread类之间的联系:

  public class Thread extends Object implements Runnable

  发现Thread类也是Runnable接口的子类。


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值