美文网首页
继承Thread类实现多线程

继承Thread类实现多线程

作者: 秋笙fine | 来源:发表于2019-01-16 00:28 被阅读0次

Thread类是一个支持多线程的功能类,只要有一个子类它就可以实现多线程。

除此之外,所有程序的起点是main方法,所有线程也有一个自己的起点,run方法,多线程的每个主体类之中都必须覆写Thread类中所提供的run()方法。

public void run()

这个方法没有返回值,表明了线程一旦开始无法停止。

下面看一个示例:

package TestDemo;



class MyThread extends Thread{
    private String name;
    public MyThread(String name){
        this.name=name;
    }
    @Override
    public void run() {
        for (int i = 0; i < 200; i++) {
            System.out.println(this.name+"==>"+i);
        }
    }
}
public class TestDemo{
    
    public static void main(String[] args) {
        MyThread thread1=new MyThread("ThreadA");
        MyThread thread2=new MyThread("ThreadB");
        MyThread thread3=new MyThread("ThreadC");

        thread1.run();
        thread2.run();
        thread3.run();

    
    }
    

}

本线程类的功能是实现一个循环输出操作,我们的线程和进程一样,都必须轮流抢占资源。多线程的执行应该是多个线程并发执行。而我们上面的结果是:


image.png

顺序执行,显然与初衷不符。因此并没有真正启动多线程,真正启动多线程的是Thread类种的start()方法。

public void start();

但是调用此方法执行的方法体是run()方法定义的。我们将方法改为start()方法。

package TestDemo;



class MyThread extends Thread{
    private String name;
    public MyThread(String name){
        this.name=name;
    }
    @Override
    public void run() {
        for (int i = 0; i < 200; i++) {
            System.out.println(this.name+"==>"+i);
        }
    }
}
public class TestDemo{
    
    public static void main(String[] args) {
        MyThread thread1=new MyThread("ThreadA");
        MyThread thread2=new MyThread("ThreadB");
        MyThread thread3=new MyThread("ThreadC");

        thread1.start();
        thread2.start();
        thread3.start();
    }
}
image.png

的确实现了并发执行的真-多线程。

疑问?为什么多线程启动不是调用run()而是调用start()方法?

我们打开jdk的安装路径,查看以下src压缩包中,Thread类中start()方法的源码:

 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 */
            }
        }
    }
    private native void start0();

首先发现start方法中throw了一个IllegalThreadStateException() ,此方法没有try catch处理,也没有在start()方法上添加throws继续向调用start方法的上层抛出,这是一个RuntimeException,属于选择性异常,如果一个线程重复启动,就会抛出这个异常。

此外在start()方法中,调用了一个start0方法,使用native进行声明。

这里就引出了一个,Java中有一门技术叫JNI,这们技术能:使用Java调用本机操作系统提供的函数。但是这样的技术有一个缺点,不能够离开特定的操作系统。

如果想要线程能够执行,需要操作系统来进行资源分配,需要由JVM负责根据不同的操作系统而实现。(JVM跨平台跨操作系统调用资源,实现多线程 因而start0方法在不同的操作系统上有不同的体现)

即JVM不仅需要实现多线程的执行代码,还要根据不同的操作系统实现资源的分配,这种跨平台实现的方法,任何一个计算机软件问题都可以通过添加一个中间件来解决,嗯= =这个中间件,就是我们的start()方法了。

相关文章

  • 2020-10-25 Java线程

    Thread类java中一个类可以通过继承Thread类实现run()实现多线程。继承thread的类通过star...

  • 多线程

    java中实现多线程操作有两种方法:继承Thread类和实现Runnable接口 一、继承Thread类 //继承...

  • Javad多线程(未)

    启动多线程的3种方式 继承Thread类、实现Runnable接口、匿名类 继承Thread类 在run方法中实现...

  • 多线程学习-第一天

    多线程学习-第一天 1 多线程的实现方式 1.1 继承Thread类 通过继承Thread类并重写run方法实现:...

  • Java多线程理解1

    Java实现多线程的两种方式分别是继承Thread类和实现Runnable接口。 继承Thread类 实现Runn...

  • java线程&和各种奇奇怪怪的锁

    1、Java线程 一、Java多线程常用的两种实现方法 1、 继承Thread类 子类继承Thread类具备多线程...

  • 3-Java中如何创建线程

    Runnable和Thread实现多线程的区别 Java中实现多线程有两种方法:继承Thread类、实现Runna...

  • 4 多线程00

    一 多线程的创建 1、thread/runnablethread:继承Thread类runnable:实现Runn...

  • 多线程概览

    一. 创建多线程的方式 1. 继承Thread类 实现步骤: 自定义类继承Thread类 重写run方法 创建类对...

  • Java多线程

    Java中创建多线程有两种方式:继承Thread类和实现Runnable接口。 继承Thread类 Java的Th...

网友评论

      本文标题:继承Thread类实现多线程

      本文链接:https://www.haomeiwen.com/subject/cuzudqtx.html