java基础

java多线程源码解读 start,run,join,yied

2017-04-16  本文已影响40人  _npc_

java
/**

* 这一段代码创建了一个线程,并打印出hello Word,
先看一下runnable接口源码吧.
```java```
/* @author  Arthur van Hoff
 * @see     java.lang.Thread
 * @see     java.util.concurrent.Callable
 * @since   JDK1.0
 */
@FunctionalInterface
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();
} ```
*  Runnable自jdk1.0定义,只定义了一个run方法。实现接口必须实现其方法,所以重写了run方法。
在看下Thread类
```java```
public
class Thread implements Runnable {
    /* Make sure registerNatives is the first thing <clinit> does. */
    private static native void registerNatives();
    static {
        registerNatives();
    }
这里有一个小疑问,既然线程有自己独立的寄存器为什么还要加锁呢?

yield()方法让出执行权
 ```java```
  public static native void yield(); //本地方法
package com.cn.threadpool;

/**
 * Created by 陈奇 on 2017/4/14 0014.
 */
public class ThreadHelloWord extends Thread {
    private String helloword;
    ThreadHelloWord(){}
    ThreadHelloWord(String helloword){
       super(helloword);
    }
    @Override
    public void run() {
        for (int i = 0; i <20 ; i++) {
            System.out.println("" + this.getName() + "-----" + i);
            // 当i为10时,该线程就会把CPU时间让掉,让其他或者自己的线程执行(也就是谁先抢到谁执行)
            if (i == 10) {
                this.yield();
            }
        }
    }

    public static void main(String[] args) {
        ThreadHelloWord h = new ThreadHelloWord("h");
        ThreadHelloWord w = new ThreadHelloWord("w");
        Thread th = new Thread(h);
        Thread tw = new Thread(w);
        th.start();
        tw.start();

    }
}
Paste_Image.png Paste_Image.png

join() 方法
源码
java
public final void join() throws InterruptedException {
join(0);
}
public final synchronized void join(long millis)
throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0;

    if (millis < 0) {
        throw new IllegalArgumentException("timeout value is negative");
    }

    if (millis == 0) {
        while (isAlive()) {
            wait(0);
        }
    } else {
        while (isAlive()) {
            long delay = millis - now;
            if (delay <= 0) {
                break;
            }
            wait(delay);          // 调用wait方法
            now = System.currentTimeMillis() - base;
        }
    }
}
join方法调用的是object的wait方法,
demo
  ```java```
/**
 * Created by 陈奇 on 2017/4/16 0016.
 */
public class JoinTest extends Thread {
    JoinTest(String name){
        super(name);
    }
    @Override
    public void run() {
        try {
            sleep(30);
            System.out.println(this.getName()+"____run ");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(new JoinTest("t"));
        Thread t1 = new Thread(new JoinTest("t1"));
        t.start();                                          // wait(0),立即执行
        t.join();
        t1.start();

        t1.join(10); //main线程等待时间
        System.out.println("main end");
    }
}
Paste_Image.png
上一篇 下一篇

猜你喜欢

热点阅读