java多线程学习——(1)认识Thread和Runnable
2017-10-23 14:14
656 查看
1.Runnable:Runnable是一个接口,该接口中仅有一个方法为run方法,如下:
2.Thread:Thread是实现了Runnable接口的方法,该类中有一个变量为
Thread方法中有一个start方法用来启动线程:
当我们使用new Thread().start()启动一个线程的时候,其调用流程为start()->start0()->run(),我们可以注意到在start()方法中会调用start0()方法,start0()方法是一个native方法,它是怎么最后调用到run()方法的呢?
我们知道,当一个类第一次被使用到时,这个类的字节码会被加载到内存,并且只会回载一次。在这个被加载的字节码的入口维持着一个该类所有方法描述符的list,这些方法描述符包含这样一些信息:方法代码存于何处,它有哪些参数,方法的描述符(public之类)等等。
如果一个方法描述符内有native,这个描述符块将有一个指向该方法的实现的指针。这些实现在一些DLL文件内,但是它们会被操作系统加载到java程序的地址空间。当一个带有本地方法的类被加载时,其相关的DLL并未被加载,因此指向方法实现的指针并不会被设置。当本地方法被调用之前,这些DLL才会被加载,这是通过调用java.system.loadLibrary()实现的。
因此,Thread类编译完成后在虚拟机中运行的时候,带有native的方法会找到该方法的实现类,然后调用run()方法。
线程的两种创建方式
1 实现Runnable接口的方式
2 继承Thread类的方式
这两种方式有何不同?
上面我们说过Thread方法实现了Runnable接口,即实现了run()方法,当通过new Thread()方法来启动线程的时候,Thread会调用被重写的run()方法,当通过Runnable方法来启动的时候new Thread(Runnable target) ,在Thread构造的时候会把自己实现的Runnable类传递给Thread中的target变量,最后调用的时候即调用this.target.run()方法。实际上这里边用到了代理模式,Thread实际上是Runnable实现类的代理,当调用start方法的时候实际上最后还是调用的是我们自己重写的run()方法。
public interface Runnable { /** * When an object implementing interface <code>Runnable</code> is used * to create a thread, starting the thread causes the object's * <code>run</code> method to be called in that separately executing * thread. * <p> * The general contract of the method <code>run</code> is that it may * take any action whatsoever. * * @see java.lang.Thread#run() */ public abstract void run(); }
2.Thread:Thread是实现了Runnable接口的方法,该类中有一个变量为
/* What will be run. */ private Runnable target;
Thread方法中有一个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(); /** * If this thread was constructed using a separate * <code>Runnable</code> run object, then that * <code>Runnable</code> object's <code>run</code> method is called; * otherwise, this method does nothing and returns. * <p> * Subclasses of <code>Thread</code> should override this method. * * @see #start() * @see #stop() * @see #Thread(ThreadGroup, Runnable, String) */ @Override public void run() { if (target != null) { target.run(); } }
当我们使用new Thread().start()启动一个线程的时候,其调用流程为start()->start0()->run(),我们可以注意到在start()方法中会调用start0()方法,start0()方法是一个native方法,它是怎么最后调用到run()方法的呢?
我们知道,当一个类第一次被使用到时,这个类的字节码会被加载到内存,并且只会回载一次。在这个被加载的字节码的入口维持着一个该类所有方法描述符的list,这些方法描述符包含这样一些信息:方法代码存于何处,它有哪些参数,方法的描述符(public之类)等等。
如果一个方法描述符内有native,这个描述符块将有一个指向该方法的实现的指针。这些实现在一些DLL文件内,但是它们会被操作系统加载到java程序的地址空间。当一个带有本地方法的类被加载时,其相关的DLL并未被加载,因此指向方法实现的指针并不会被设置。当本地方法被调用之前,这些DLL才会被加载,这是通过调用java.system.loadLibrary()实现的。
因此,Thread类编译完成后在虚拟机中运行的时候,带有native的方法会找到该方法的实现类,然后调用run()方法。
线程的两种创建方式
1 实现Runnable接口的方式
public class ThreadDemo implements Runnable{ @Override public void run() { System.out.println("start thread by Runnable"); } }
2 继承Thread类的方式
public class ThreadDemo extends Thread{ @Override public void run() { System.out.println("start thread by Thread"); } }
这两种方式有何不同?
上面我们说过Thread方法实现了Runnable接口,即实现了run()方法,当通过new Thread()方法来启动线程的时候,Thread会调用被重写的run()方法,当通过Runnable方法来启动的时候new Thread(Runnable target) ,在Thread构造的时候会把自己实现的Runnable类传递给Thread中的target变量,最后调用的时候即调用this.target.run()方法。实际上这里边用到了代理模式,Thread实际上是Runnable实现类的代理,当调用start方法的时候实际上最后还是调用的是我们自己重写的run()方法。
相关文章推荐
- Java多线程学习-Runnable和Thread的区别
- Java多线程高并发学习笔记(一)——Thread&Runnable
- Java多线程--认识多线程(Thread、Runnable)
- java入门学习:多线程创建-Thread,Runnable,callable和threadpool
- Java学习从菜鸟变大鸟之三 多线程中Thread 和Runnable的区别与运用
- 学习java多线程的笔记1--Thread(Runnable t)与重写run()方法等
- Java学习从菜鸟变大鸟之三 多线程中Thread 和Runnable的区别与运用
- java多线程学习1-继承Thread类和实现Runnable接口来创建线程
- java创建多线程的两种方法(继承Thread类/实现Runnable接口)
- java多线程 基础(二) Thread Runnable
- java 多线程 Thread和Runnable的区别
- Java对多线程的支持(一) - Thread类、Runnable接口、Daemon线程、线程同步
- Java学习札记5:Thread 和 Runnable 的联系和区别(1)
- Java 多线程的Thread类和Runnable接口
- 【Java多线程学习】Java多线程的两种实现方式:继承Thread类 & 实现Runable接口
- java多线程 Thread 和Runnable
- Java【多线程知识总结(5)】比较继承Thread类创建线程和实现Runnable接口创建线程这两种方式
- JAVA多线程学习Runnable接口
- java 三种多线程的创建方法Thread,Handler,Runnable
- JAVA基础(多线程Thread和Runnable的使用区别