多线程初学习

  首先,我们需要知道,在Java中实现多线程的方式有三种:1、继承Thread类;2、实现Runnable;3、实现Callable; 

 Runnable接口:
package java.lang;

    @FunctionalInterface
    public interface Runnable
    {
        public abstract void run();
    }

   Thread类:

package java.lang;
public
class Thread implements Runnable {
/* What will be run. */
    private Runnable target;
 private void init(ThreadGroup g, Runnable target, String name,
                      long stackSize, AccessControlContext acc) {
    .........
        this.target = target;
    ........
    }
 public Thread(Runnable target) {
        init(null, target, "Thread-" + nextThreadNum(), 0);
    }
 public synchronized void start() {
    ........
}
 
    @Override
    public void run() {
        if (target != null) {
            target.run();
        }
    }

   源码里可以看出,其实Thread也是实现了Runnable接口,用start方法调用Thread对象的时候,调用实现Runnable接口中所实现的run()方法,从这里入手,我们可以慢慢的去搞懂这个过程是怎么实现的。

原文地址:https://www.cnblogs.com/zhang-yawei/p/10272387.html